Skip to content

Decorator self-override operations not allowed for user-defined classes? #9637

Answered by erictraut
NCBM asked this question in Q&A
Discussion options

You must be logged in to vote

The property class requires extensive special-casing within type checkers and is exempted from redeclaration checks.

I recommend against using this pattern in your own classes if you want them to work well with static typing. If you choose to use this pattern, you'll need to handle the ramifications of the redeclarations.

One workaround is to avoid using the same symbol name for different type declarations, like this:

from typing import Callable, Self

class A[T]:
    def do_nothing(self, b: T) -> Self: ...
    def replace[Tx](self, b: Tx) -> "A[Tx]": ...

a1 = A[Callable[[], None]]()

@a1.do_nothing
def a2() -> None: ...

@a2.replace
def a(foo: int) -> None: ...

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by NCBM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants