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

Class variable gets the type of instance variable #4886

Open
roganov opened this issue Apr 11, 2018 · 4 comments
Open

Class variable gets the type of instance variable #4886

roganov opened this issue Apr 11, 2018 · 4 comments
Labels
topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope

Comments

@roganov
Copy link

roganov commented Apr 11, 2018

According to PEP 526, type annotations inside class bodies apply to instances unless wrapped with ClassVar. Am I misreading something or is this a bug?

class Test:
    a: int

reveal_type(Test.a + 1)
Revealed type is 'builtins.int'
@ilevkivskyi
Copy link
Member

According to PEP 526, type annotations inside class bodies apply to instances unless wrapped with ClassVar. Am I misreading something or is this a bug?

So where is the contradiction here? The PEP doesn't say instance variables can't be set on the class. This is a relatively common pattern -- instance variable with a default (also mentioned in the PEP IIRC). The opposite however is unsafe, and therefor prohibited:

class Test:
    a: ClassVar[int]

Test().a = 1  # Error!

@PeterJCLaw
Copy link
Contributor

@ilevkivskyi I think that @roganov's point is the reverse -- not that the member shouldn't be inherited by the instance, rather that the member should only be present on the instance.

@roganov
Copy link
Author

roganov commented May 3, 2018

Ideally there should be a way to specify different types for class members and instance members. I tried to implement this using descriptors, but didn't succeed.

@KotlinIsland
Copy link
Contributor

The pep is slightly vague about this:

Type annotations can also be used to annotate class and instance variables in class bodies and methods. In particular, the value-less notation a: int allows one to annotate instance variables that should be initialized in __init__ or __new__.
- pep 526

While this does indicate that it shouldn't be available on the class, it doesn't explicitly rule it out.

The case from #11832 is very cut and dry though. Where the instance variables are declared within the __init__ body.

class Foo:
    def __init__(self) -> None:
        self.bar: str
        self.foo = 1
        
Foo.foo # no error
Foo.bar # no error

@AlexWaygood AlexWaygood added topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope labels Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope
Projects
None yet
Development

No branches or pull requests

5 participants