Skip to content

Need help writing a generic class with an overloaded property #8836

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

You must be logged in to vote

I wouldn't recommend designing a class this way. If this code is going to be maintained over time, you may want to invest in a redesign.

If you don't want to modify the code, you'll need to use some unsavory hacks to make it type check. The tool I'd reach for in this case is TYPE_CHECKING. This makes for very fragile code, but given the constraints, you don't have many options.

    if TYPE_CHECKING:
        calc: _CalcDescriptor
    else:
        @property
        def calc(self): ...

Code sample in pyright playground

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mfisher87
Comment options

Answer selected by mfisher87
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