mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Confusing error when subclassing `var.__class__`

Open denballakh opened this issue 2 years ago • 1 comments

Consider this example:

x: int = 1
class X(x.__class__):
    # Name "x.__class__" is not defined
    # Class cannot subclass "__class__" (has type "Any")
    pass

Name "x.__class__" is not defined is confusing. Why x.__class__ is considered a name? I suggest removing this error in this case.

My Environment

  • mypy 0.961 (compiled: no)
  • no command-line flags, no config file
  • CPython 3.10.4
  • Windows 10

denballakh avatar Jul 03 '22 14:07 denballakh

is this related to this issue: https://github.com/python/mypy/issues/4177 ?

thoroc avatar Sep 15 '22 12:09 thoroc

~Yes, this is a duplicate of https://github.com/python/mypy/issues/4177.~

Edit: As Alex points out below, this isn't a duplicate.

erictraut avatar Aug 13 '23 16:08 erictraut

Yes, this is a duplicate of #4177.

No, it's not a duplicate, I don't think. This issue is about mypy not recognising the object.__class__ attribute. #4177 is about mypy not recognising the magic __class__ variable that is implicitly inserted into the scope of all methods in Python.

AlexWaygood avatar Aug 13 '23 16:08 AlexWaygood