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

Structural subtyping: unawareness of special attributes of user-defined functions. #10403

Closed
tyralla opened this issue May 3, 2021 · 1 comment · Fixed by #14084
Closed
Labels
bug mypy got something wrong topic-protocols

Comments

@tyralla
Copy link
Collaborator

tyralla commented May 3, 2021

User-defined functions come, among others, with the special attribute __name__. Mypy 0.812 correctly reveals the type str for attribute __name__ of function f in the following example. Still, it states an assignment error when trying to handle f as a subtype of the callback protocol A, which also defines a __name__ attribute of type str.

from typing import Protocol

class A(Protocol):
    __name__: str
    def __call__(self) -> None: ...

def f() -> None: ...

reveal_type(f.__name__)  # note: Revealed type is 'builtins.str'
a: A = f  # error: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "A")  [assignment]

This problem also occurs for all other special attributes I checked (e.g. __doc__).

@Cheaterman
Copy link

Apologies again for creating a duplicate ; sorry for the noise as well, but I'm guessing some activity could help with searchability :-) thanks for all the great work!

ilevkivskyi added a commit that referenced this issue Nov 13, 2022
Fixes #10976
Fixes #10403

This is quite straightforward. Note that we will not allow _arbitrary_
attributes on functions, only those that are defined in
`types.FunctionType` (or more precisely `builtins.function` that is
identical). We have a separate issue for arbitrary attributes
#2087
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-protocols
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants