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

Display dict of namedtuples #230

Open
robertvazan opened this issue Mar 3, 2020 · 6 comments
Open

Display dict of namedtuples #230

robertvazan opened this issue Mar 3, 2020 · 6 comments

Comments

@robertvazan
Copy link

@robertvazan robertvazan commented Mar 3, 2020

Fire nicely prints a dict of dicts using one line per inner dict. When I try to do the same with dict of namedtuples (which otherwise behave like dicts after #161), I just get a help page listing all the dict keys. When I specify a dict key, then that namedtuple is printed. I would prefer all namedtuples in the dict to be printed when no key is specified.

@robertvazan
Copy link
Author

@robertvazan robertvazan commented Mar 3, 2020

BTW, it would be probably a good idea to display namedtuple itself like a dict, but then this somewhat depends on what namedtuples are commonly used for. Mine would be more readable with field labels.

@dbieber
Copy link
Member

@dbieber dbieber commented Mar 3, 2020

Good idea.

The relevant code to change is:

python-fire/fire/core.py

Lines 258 to 261 in c830177

elif isinstance(result, dict) and value_types.IsSimpleGroup(result):
print(_DictAsString(result, verbose))
elif isinstance(result, tuple):
print(_OneLineResult(result))

and

def IsSimpleGroup(component):

@armingh2000
Copy link

@armingh2000 armingh2000 commented Mar 16, 2020

I've been working on this issue for a while.I think that namedtuples are not correctly handled yet.
In inspectutils.py, the function IsNamedTuple is implemented wrong. Because namedtuple is not an instance of tuple ( isinstance(a namedtuple variable, tuple) is false). I wonder how it works on some namedtuples?
If we want to handle namedtuples, are we focusing on only collections module or also other modules as well?

@dbieber
Copy link
Member

@dbieber dbieber commented Mar 17, 2020

Because namedtuple is not an instance of tuple ( isinstance(a namedtuple variable, tuple) is false).

I just confirmed in Python 3:

In [2]: import collections                                                                                                                                         
In [3]: Point = collections.namedtuple('Point', ['x', 'y'])                                                                                                        
In [4]: x = Point(1,2)                                                                                                                                             
In [5]: isinstance(x, tuple)
Out[5]: True

Where did you see a namedtuple that was not an instance of tuple?

If we want to handle namedtuples, are we focusing on only collections module or also other modules as well?

Are there other collections or types you're thinking about? We don't have a clearly articulated scope. NamedTuples are important to handle well because they're commonly used.

@armingh2000
Copy link

@armingh2000 armingh2000 commented Mar 17, 2020

I just confirmed in Python 3:
In [2]: import collections
In [3]: Point = collections.namedtuple('Point', ['x', 'y'])
In [4]: x = Point(1,2)
In [5]: isinstance(x, tuple)
Out[5]: True

Yes. You're right. I made a mistake in passing the right variable to it.

Are there other collections or types you're thinking about? We don't have a clearly articulated scope. NamedTuples are important to handle well because they're commonly used.

I've used typing.NamedTuple before. Which is a typed version of collection.namedtuple:(https://docs.python.org/3/library/typing.html#typing.NamedTuple)

It's Handled well here, like a collections.namedtuple object.

armingh2000 added a commit to armingh2000/python-fire that referenced this issue Mar 17, 2020
@armingh2000
Copy link

@armingh2000 armingh2000 commented Apr 9, 2020

@dbieber
I made a simple change and made a pull request. I think it'll do the job. Can you check?

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.