In Python, 如何为必须是类的实例或其子类的变量添加类型提示?

huangapple go评论56阅读模式
英文:

How to Type Hint a variable that must be an instance of a class or of its subclasses in Python?

问题

Sure, here's the translated content:

让我们假设我有一个名为Animal的类以及DogCatAnimal的子类。
我想要类型提示一个名为a的变量,以声明它既可以是Animal的实例,也可以是Animal实例的子类之一。

  • 我尝试进行类型提示的示例:
class Animal:
    pass

class Dog(Animal):
    pass

class Cat(Animal):
    pass

a: type[Animal] = Dog() #但也可以是Animal()或Cat()

如您所见,我一直在使用这种语法:type[ParentClassName]
但是,根据我在类型提示文档中阅读的内容,type[ParentClassName] 用于类,而不是类的实例(参见:https://docs.python.org/3/library/typing.html#typing.Type)。

(请注意,type[ClassName]与Python 3.9以后的版本中的typing.Type[ClassName]是相同的)

我正在寻找类型提示的主要重点是它既可以是Animal的实例,也可以是Dog/Cat的实例,同时也符合mypy的要求。

  • 所以是否有一种方法可以通过类型提示来实现这一点?

对于任何错误,对不起,英语不是我的母语。
感谢任何尝试回答我的问题的人。

英文:

Let's say that I have a class Animal and some subclasses of Animal like Dog and Cat.
I want to type hint a variable named a in order to declare that it can be both an Animal's instance or one of the subclass of Animal instance.

  • Example of me trying to type hint :
class Animal :
   pass

class Dog(Animal) :
   pass

class Cat(Animal) :
   pass

a : type[Animal] = Dog() #But could have been Animal() or Cat()

As you can see I have been using this syntax : type[ParentClassName] .
However, as I have read on the typing documentation, type[ParentClassName] is for a class and not instances of a class (see : https://docs.python.org/3/library/typing.html#typing.Type).

(Note that type[ClassName] is the same as typing.Type[ClassName] since Python 3.9)

The main focus of the type hint I'm searching for is the fact that it can be both an Animal's instance or a Dog/Cat's instance while being mypy friendly.

  • So is there a way to do this by type hinting ?

Sorry for any mistakes, english isn't my first language.
And thanks to anyone who will try to answer my question.

答案1

得分: 0

首先,我的目标是声明变量 a 是一个 Animal

但我担心的是,如果 a 成为 Dog 的一个实例,即使 Dog 继承自 Animala 仍然会被视为一个 Animal

答案是肯定的,所以使用类型提示来声明的方式是:

class Animal:
    pass

class Dog(Animal):
    pass

a: Animal = Dog()

实际上,这种正确性来自于在 PEP 483 中描述的 子类型关系 的概念(请参阅:https://peps.python.org/pep-0483/#subtype-relationships)。

感谢 Brian61354270juanpa.arrivillaga 提供的答案。

英文:

Firstly my goal was to have a way to declare that my variable a is an Animal.
However my concern was that if a become an instance of Dog , even if Dog inherit from Animal, would a still be considered an Animal.

The answer is yes and so the way to declare it with type hint is :

class Animal :
    pass

class Dog(Animal) :
    pass

a: Animal = Dog()

In fact the reason why it's correct come from the Subtype relationships notion described in PEP 483 (see : https://peps.python.org/pep-0483/#subtype-relationships).

Thanks to Brian61354270 and juanpa.arrivillaga for their answers.

huangapple
  • 本文由 发表于 2023年4月17日 08:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定