Skip to content

Change in behavior regarding __getattr__ and @no_type_check #13319

Closed
@PhilReinhold

Description

@PhilReinhold

Bug Report

In #6251 it was recommended to use @no_type_check on __getattr__ if you wanted errors to still be raised for missing attributes. However this appears to no longer work since v0.930

To Reproduce

# test.py
from typing import no_type_check

# Using no_type_check decorator on __getattr__
class Base:
    @no_type_check
    def __getattr__(self, name):
        pass

class Model(Base):
    age: int


m = Model()
reveal_type(m.foo)

# Without no_type_check decorator on __getattr__
class Base2:
    def __getattr__(self, name):
        pass

class Model2(Base2):
    age: int


m2 = Model2()
reveal_type(m2.foo)

Expected Behavior

Following the discussion on #6251, I expected that adding a @no_type_check decorator would restore errors on missing attributes. I see this behavior on several versions <= 0.921

When running mypy test.py on version 0.921 I get the expected output

test.py:13: error: "Model" has no attribute "foo"
test.py:13: note: Revealed type is "Any"
test.py:24: note: Revealed type is "Any"

Actual Behavior

When running on versions 0.930-0.971 I do not see the (expected) first error indicating the missing attribute,

test.py:13: note: Revealed type is "Any"
test.py:24: note: Revealed type is "Any"

Your Environment

  • Mypy version used: Multiple, error appears on at least v0.930 v0.931 v0.941 v0.971 and not before
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.4
  • Operating system and version: OSX 11.6.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions