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

mypy doesn't understand @abc.abstractmethod on __getattr__ #10409

Closed
Tracked by #11142
hotpxl opened this issue May 4, 2021 · 1 comment · Fixed by #11150
Closed
Tracked by #11142

mypy doesn't understand @abc.abstractmethod on __getattr__ #10409

hotpxl opened this issue May 4, 2021 · 1 comment · Fixed by #11150
Labels
bug mypy got something wrong

Comments

@hotpxl
Copy link

hotpxl commented May 4, 2021

Bug Report

mypy doesn't understand

    @abc.abstractmethod
    def __getattr__(self, name:str) -> Any:

and complains about missing attributes

To Reproduce

Run mypy on:

import abc
from typing import *

class CC(abc.ABC):
    @abc.abstractmethod
    def __getattr__(self, name:str) -> Any:
        ...

def foo(c: CC) -> None:
    print(c.hello)

Expected Behavior

mypy shouldn't complain about attribute "hello" because I've overridden __getattr__. mypy exhibits the correct behavior when the @abc.abstractmethod decorator is removed.

Actual Behavior

mypy complains that main.py:11: error: "CC" has no attribute "hello". But if I comment out @abc.abstractmethod mypy will not complain.

Your Environment

  • Mypy version used: mypy 0.820+dev.8bc1115a7469ac4702d29da87acf5831c696d007 (just pip installed from git)
  • Mypy command-line flags: mypy main.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.7.3
  • Operating system and version: Debian GNU/Linux 10
@hotpxl hotpxl added the bug mypy got something wrong label May 4, 2021
@hotpxl hotpxl changed the title mypy doesn't understand @abc.abstractmethod __getattr__ mypy doesn't understand @abc.abstractmethod on __getattr__ May 4, 2021
@finite-state-machine
Copy link

This is also true for __getattribute__, and any decorator.

For example,

from typing import (
        Any,
        Callable,
        TypeVar,
        )


T = TypeVar('T', bound=Callable[..., Any])


def labeled(function: T) -> T:
    function.__labeled__ = True # type: ignore[attr-defined]
    return function


class One:
    @labeled
    def __getattribute__(self, _: str) -> str:
        return "hello"


def function() -> None:

    one = One()
    print(one.foo) # error: "One" has no attribute "foo" [attr-defined]

JukkaL pushed a commit that referenced this issue Dec 3, 2021
Support decorators properly in additional contexts.

Closes #10409
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this issue Jan 20, 2022
Support decorators properly in additional contexts.

Closes python#10409
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants