Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support case-insensitive usage of Python Fire CLIs #43

Open
dbieber opened this issue Mar 28, 2017 · 12 comments
Open

Support case-insensitive usage of Python Fire CLIs #43

dbieber opened this issue Mar 28, 2017 · 12 comments

Comments

@dbieber
Copy link
Member

dbieber commented Mar 28, 2017

Much like how we allow users to use hyphens - in place of underscores, this Issue is to allow users to use all-lowercase versions of identifiers.

Typical case

import fire
class CaseExample(object):
  def Alpha():
    return 'path 1'
fire.Fire(CaseExample)
$ python example.py Alpha  # This is the current behavior.
path 1
$ python example.py alpha  # This is the new behavior I want to add.
path 1
$ python example.py ALPHA   # Do we want to support other capitalizations? Or just the all lower-case version?
path 1

Corner cases
Unlike with the -/_ support, there can potentially be members with multiple capitalization variants of the name.

E.g.

import fire
class CaseExample(object):
  def alpha():
    return 'path 1'
  def Alpha():
    return 'path 2'

fire.Fire(CaseExample)

If the user provides an exact match, it's clear what we should do. I'm open to discussion about how to handle the ambiguous case.

$ python example.py alpha
path 1
$ python example.py Alpha
path 2
$ python example.py ALPHA
WARNING: Ambiguous member access `ALPHA`; accessing `alpha`.
path 1

Why?
The primary motivation is that command line tools typically use lower case commands, whereas Python classes (and occasionally methods/functions) typically have capital names. By allowing these capital names to be used via their lower-case variant, the CLIs that result from normal python code will feel natural more often.

Possible extension
If we also support dropping/adding -/_s in some situations, then people could use snake-case to refer to CamelCase items... just brainstorming. Thoughts?

@dbieber
Copy link
Member Author

dbieber commented Mar 28, 2017

issue courtesy of @zqureshi via https://twitter.com/zeeshanq/status/838604546389733378 :)

@zqureshi
Copy link

Depending when fire inspects the objects to construct a lookup table, it could emit a warning as soon as it detects collisions. Always defaulting to lowercase with a warning seems like a reasonable default.

@zqureshi
Copy link

You could add a config flag that errors out if collision detected for people who want to be sure.

@dbieber
Copy link
Member Author

dbieber commented Mar 31, 2017

I don't think we'll want to add such a flag. In the majority of cases it will be clear what to do, and we'll just want to pick a sensible way to handle the occasional ambiguity.

To be clear, the ambiguous case is only when an object has multiple members that have the same name but different capitalizations, and the end users enters a third capitalization that's different from any of the actual capitalizations. In this case, I'm leaning toward simply showing a warning and exiting rather than trying to guess (e.g. via edit distance) what the user was trying to do.

@merriam
Copy link

merriam commented Sep 27, 2017

As a quick drive by comment, this is as much about the functionality of Python as anything else. This is a constant loss of small amounts of programming time. Specifically, that "a_class", "aclass", and "aClass" are each distinct.

I would recommend that the Fire API give a better error message, such as:

'Alpha_Dog' not found.   Did you mean 'alphaDog'?   

Meaning that a valid option matched except for case, underscores, and, possibly, spaces.

@Mark-Jung
Copy link

Hey guys, is anyone working on this issue? If not, I wanted to take a stab at it.

@dbieber
Copy link
Member Author

dbieber commented Aug 22, 2018

Go for it. :)

I know there was some ambiguity in the original issue description, so here's what I think we should do. I think we should support precisely the exact match case and the all-lowercase case. If the all-lowercase case is ambiguous (and not also an exact match), then we'll show a usage error. How does that sound to you?

@Mark-Jung
Copy link

Sounds awesome. It'll be my first open source contribution so I'm a bit nervous but wish me luck! haha

@dbieber
Copy link
Member Author

dbieber commented Aug 22, 2018

Welcome to the open source world!

A few notes:

  • There's no rush on this, it doesn't have to make it into the next release. This could take some time to get right.
  • Please try to over-communicate. Let us know what you're trying, what worked, what didn't work, any questions you have about the codebase, etc. Doing all this in the open is great, but if for some reason you want to say something not for the whole world to see, that's an option too: [email protected]

Good luck!

@Mark-Jung
Copy link

Hey, sorry I've been away for some time but I looked at this issue again today and just wanted to check up on my understanding of the code and validity of my approach for this issue.

I assumed the main code base I'll be touching will be in the core.py, specifically inside the_Fire function. The part that interested me were the if statements starting from line 400. They were either calling and adding it to the trace or adding it to the accessed property depending on the type of the component. I'm still not sure where to check for case sensitive calls but my best guess is to

  1. Change the GetFileAndLine function in inspectutils.py to find the filename and line number of the component for the component even when the case is off.

  2. Change the code inside the last if statement where the component is a dict. I think it should look for the member case insensitively.

I'm going to try these two things later today. I'm still learning about the codebase so if y'all find something glaringly off or think there's some better way to solve this issue, please don't hesitate to mention it! I'm very open to opinions and guidance. Hope y'all have a good day.

@vikramsubramanian
Copy link

May I take a shot at this?

@flaviaouyang
Copy link

is this issue still relevant? if so, i’d like to work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants