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

mypy can't find type defined inside a function. #4349

Closed
asincero opened this issue Dec 12, 2017 · 6 comments
Closed

mypy can't find type defined inside a function. #4349

asincero opened this issue Dec 12, 2017 · 6 comments
Labels

Comments

@asincero
Copy link

Steps to reproduce

mypy can't seem to find a class defined inside a function. For example:

import typing

def foo() -> typing.Any:
    class Bar(typing.NamedTuple):
        member: str

    o = Bar(member='fee')
    return o

Current behavior

I get the following when I run mypy on the above code (saved in a file named "t1.py"):

t1.py:4: error: Name 'Bar' is not defined
t1.py:7: error: Missing positional arguments "typename", "fields", "verbose", "rename", "kwargs" in call to "Bar"

Expected behavior

No errors or warning from mypy.

mypy --version output

mypy 0.540

@gvanrossum
Copy link
Member

Good catch. It appears subclassing NamedTuple is only recognized at the module level, not inside a function. So your workaround would be to move Bar up to the module level.

@ilevkivskyi
Copy link
Member

...or you can just use (less nice) functional form Bar = NamedTuple('Bar', [('member', str)]) which works correctly.

This means that this bug should be easy to fix.

@gvanrossum
Copy link
Member

My guess is the bug has to do with some pass not descending into function bodies. We've seen such errors before.

@solomatov
Copy link

Are there any updates on this?
I still have it in 0.660

@momohatt
Copy link
Contributor

This doesn't reproduce in 0.782. Maybe it's already fixed?

@97littleleaf11
Copy link
Collaborator

I cannot reproduce it in latest version. Seems it had been fixed.

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

No branches or pull requests

6 participants