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

Disallow access to instance variable via class #240

Open
JukkaL opened this issue Jul 15, 2013 · 5 comments
Open

Disallow access to instance variable via class #240

JukkaL opened this issue Jul 15, 2013 · 5 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 15, 2013

Currently an instance variable can be accessed using the class, which is wrong. For example, this code is accepted by the type checker:

class A:
    def f(self) -> None:
        self.x = 1
A.x = 1 # accepted, but should be an error

Class variables need to be assigned in the class body:

class A:
    x = 1
A.x = 1 # ok, and should be ok
@JukkaL JukkaL removed the front end label Jul 25, 2014
@JukkaL
Copy link
Collaborator Author

JukkaL commented May 17, 2015

Actually, I'm no longer convinced that this is a bug. Turning this into an enhancement proposal.

@JukkaL JukkaL added needs discussion and removed bug mypy got something wrong labels May 17, 2015
@gvanrossum
Copy link
Member

Yeah, this may just be a somewhat unconventional wy of setting the default.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 13, 2015

Postponing this as I'm not sure if this is a good idea but might still be worth considering.

@mark-kubacki
Copy link

mark-kubacki commented Feb 18, 2022

The OP has it as writing—I ran into this erroneously reading a variable; consider:

from __future__ import annotations
import logging

from typing import Type


class A:
    def __init__(self):
        self._log = logging.getLogger()
        pass

    @classmethod
    def from_spam(cls: Type[A], spam):
        if spam:
            cls._log.warning("got spam")  # accepted, but should be an error
        return cls()

@gsakkis
Copy link

gsakkis commented Sep 11, 2023

I ran into a situation which is almost the opposite problem of the original one: not only a given attribute is deliberately accessible by both the instance and the class, but the type in each case different. Unsurprisingly mypy complains about Incompatible types in assignment.

Minimal example:

class A:
    x = "a"

    def __init__(self) -> None:
        self.x = 1

assert A().x == 1
assert A.x == "a"

I tried annotating x both as an instance and class variable but this caused a Name "x" already defined error without getting rid of the first one:

class A:
    x: int
    x: ClassVar[str] = "a"

    def __init__(self) -> None:
        self.x = 1

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

4 participants