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

Prohibit use of names in annotations that are undefined in runtime #3088

Closed
pkch opened this issue Mar 30, 2017 · 2 comments
Closed

Prohibit use of names in annotations that are undefined in runtime #3088

pkch opened this issue Mar 30, 2017 · 2 comments

Comments

@pkch
Copy link
Contributor

pkch commented Mar 30, 2017

This type checks, but crashes in runtime with NameError:

class A:
    def f(x: A) -> None:
        pass

I think it's better if mypy reported an error Name "A" should be a forward reference.

@gvanrossum
Copy link
Member

Oh, that's weird. I didn't even know mypy accepts that! Agreed it would be better to flag as error. (Outside stubs, and not in type comments of course.)

Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Jul 5, 2017
This partially, but not completely, resolves
python#3088

Previously, code of the following form was accepted by mypy:

    class A:
        def foo(self) -> A:
            ...

This results in an error at runtime because 'A' is not yet defined.
This pull request modifies the parsing process to report an error when
a type is being incorrectly used within its class definition.

However, this commit does *not* attempt to handle other cases where
the user is using a type that is undefined at runtime. For example, mypy
will continue to accept the following code without an error:

    def f() -> A:
        ...

    class A:
        ...

I decided it would probably be best to address this in a future pull
request.
@JukkaL JukkaL changed the title Prohibit use of names undefined in runtime Prohibit use of names in annotations that are undefined in runtime Sep 18, 2017
@ethanhs
Copy link
Collaborator

ethanhs commented Nov 4, 2017

Closing as a duplicate in favor of #948, which has a bit more information.

@ethanhs ethanhs closed this as completed Nov 4, 2017
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

5 participants