最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Is there a way to annotate a function with two generic arguments indicating that both type variables are bound to the s

matteradmin1PV0评论

I would like to write a function accepting two arguments that are supposed to be compatible with the same base class:

def example_function(desired_type: type[DesiredType], base_object: RelatedType) -> DesiredType:
    ...

where RelatedType and DesiredType should be type variables bound to the same parent class (which is known only when the function is called with concrete arguments).

A possible example of such function might be a factory class-method able to create an instance of the same class based on some attributes of another object supposed partially "compatible" (in a certain context):

class Example:

    @classmethod
    def (cls: type[SelfType], existing_object: OtherType) -> SelfType:
        ...

SelfType = TypeVar("DerivedType", bound=Example)
OtherType = TypeVar("DerivedType", bound=Example)

but OtherType appears only once in the generic function signature so it is marked as reportInvalidTypeVarUse error. In this example, an easy workaround would be substituting OtherType with Example itself, which naturally complies with its subclasses.

I understand inheritance relationship cannot be enforced between different type variables, i.e. something like:

BaseType = TypeVar("BaseType")
DerivedType = TypeVar("DerivedType", bound=BaseType)

because a generic type cannot be an upper bound.

What is the correct way to provide meaningful type hints that convey the intended relationship?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far