Type derivation for a field of the dataclass containing a factory function #10677
Answered
by
UnboundVariable
Renes Noah (noahrenes)
asked this question in
Q&A
|
Hello. Is there a way to make Pyright see "the meaning in this context" of ValidatorRetType = TypeVar("ValidatorRetType")
@dataclass
class Parameter:
name: str
validate: Callable[[str, str], ValidatorRetType]
value: ValidatorRetType = field(init=False)(aside from using If possible, I would like to get a solution that works with Python 3.9. |
Answered by
UnboundVariable
Jul 4, 2025
Replies: 1 comment 1 reply
|
According to the Python typing spec, a type variable is valid only when used in the context of a "def", "class" or type alias declaration. It must be associated with one of those scopes and has no meaning if used otherwise. That's why you're seeing the error here. I'm not sure what your intent is here. Do you mean for the @dataclass
class Parameter(Generic[ValidatorRetType]):
name: str
validate: Callable[[str, str], ValidatorRetType]
value: ValidatorRetType = field(init=False) |
1 reply
Answer selected by
erictraut
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to the Python typing spec, a type variable is valid only when used in the context of a "def", "class" or type alias declaration. It must be associated with one of those scopes and has no meaning if used otherwise. That's why you're seeing the error here.
I'm not sure what your intent is here. Do you mean for the
Parameterclass to be generic and parameterized by the type variable?