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

class decorators that change the type don't work #11117

Closed
DetachHead opened this issue Sep 16, 2021 · 4 comments
Closed

class decorators that change the type don't work #11117

DetachHead opened this issue Sep 16, 2021 · 4 comments
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Sep 16, 2021

def deco(value) -> int:
    ...
    
@deco
class Class:
    ...
    
reveal_type(Class) # note: Revealed type is "def () -> __main__.Class"

@deco
def function(): ...

reveal_type(function) # note: Revealed type is "builtins.int"

https://mypy-play.net/?mypy=latest&python=3.10&gist=705baa296d3181f0b670416a405978b4

@DetachHead DetachHead added the bug mypy got something wrong label Sep 16, 2021
@CyanoKobalamyne
Copy link

CyanoKobalamyne commented Sep 29, 2021

Are there any plans for fixing this? I'm using a class decorator that is a class itself (thereby transforming the original class into an instance of the decorator), and running into issues where mypy is inferring the wrong type.

Here is a simplified example:

from typing import Generic, Type, TypeVar

T = TypeVar("T")

class Deco(Generic[T]):
    def __init__(self, cls: Type[T]):
        self._cls = cls

    def get(self) -> T:
        return self._cls()

@Deco
class Klass:
    ...

reveal_type(Deco)  # note: Revealed type is 'def [T] (cls: Type[T`1]) -> __main__.Deco[T`1]'
reveal_type(Deco.get)  # note: Revealed type is 'def [T] (self: __main__.Deco[T`1]) -> T`1'
reveal_type(Klass)  # note: Revealed type is 'def () -> __main__.Klass'
reveal_type(Klass.get)  # error: "Type[Klass]" has no attribute "get" 

DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 15, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 18, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 20, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 26, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 28, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 28, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 28, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 28, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 31, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 31, 2022
DetachHead added a commit to DetachHead/cli-tools that referenced this issue May 31, 2022
@JelleZijlstra
Copy link
Member

Duplicate of #3135

@JelleZijlstra JelleZijlstra marked this as a duplicate of #3135 Oct 9, 2023
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Oct 9, 2023
@DetachHead
Copy link
Contributor Author

DetachHead commented Oct 10, 2023

@JelleZijlstra that looks like a different issue that's since been fixed, since mypy now shows an error on invalid class decorators:

decorator: int = 3

@decorator  # error: "int" not callable  [operator]
class A:
    pass

playground

my issue is that class decorators aren't able to change the type of the class they're decorating:

def decorator(cls: type[object]) -> int: ...


@decorator  
class A:
    pass


reveal_type(A) # `def () -> __main__.A`, should be `int`

so i suggest either closing the original issue and re-opening this one, or updating the original issue with a new example (and maybe change its title)

@SeeFrenchDev
Copy link

same issue, does someone knows how to fix it?

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

No branches or pull requests

4 participants