用于区分类型别名与实际类别的约定?

[英]Convention to distinguish type aliases from actual classes?


The typing module allows assigning complex type signatures to aliases, which can then be used interchangeably with the actual type. This seems to have the problem that it confuses type aliases with actual classes, i.e. in a definition like this

输入模块允许将复杂类型签名分配给别名,然后可以与实际类型互换使用。这似乎有一个问题,即它将类型别名与实际类混淆,即在这样的定义中

def foo(obj: MyComplexObject):
    ...

there is no way to tell if MyComplexObject is implemented as a class somewhere or is just a type alias. This seems to be an unfortunate source of confusion to me, especially when the alias is (re)used far from its original definition. Is that generally accepted or is there a convention to somehow distinguish type aliases from actual classes?

没有办法判断MyComplexObject是作为某个类实现的,还是只是一个类型别名。这对我来说似乎是一个令人遗憾的混淆源,特别是当别名(重新)远离其原始定义时。这是普遍接受的还是有某种惯例以某种方式区分类型别名和实际类?

2 个解决方案

#1


2  

I believe type aliases and regular types are indistinguishable by design: type aliases are meant to be exact stand-ins for whatever type they're aliasing.

我认为类型别名和常规类型在设计上是无法区分的:类型别名对于它们是别名的任何类型都是准确的替身。

So, to use your example, it doesn't necessarily matter if MyComplexObject is an actual class or a type alias: they both behave equivalently at runtime and static typecheck time.

因此,要使用您的示例,MyComplexObject是实际类还是类型别名并不一定重要:它们在运行时和静态类型检查时都表现相同。

And because the type checker can give you immediate feedback on whether or not you're using obj in a type-safe way, there are minimal opportunities to get confused: either you'll use the obj parameter correctly or you'll get immediate feedback otherwise. (And similarly, IDEs such as Pycharm that understand PEP 484 semantics will also understand type aliases and will correctly auto-complete/flag mistakes.)

并且因为类型检查器可以为您提供关于您是否以类型安全方式使用obj的即时反馈,所以很少有机会混淆:要么您将正确使用obj参数,要么您将获得即时反馈除此以外。 (同样地,理解PEP 484语义的诸如Pycharm之类的IDE也将理解类型别名并且将正确地自动完成/标记错误。)

It's also worth noting that in the case where you specifically alias other classes, the alias and the original class are literally indistinguishable at runtime: they're both variables that refer to the same underlying type object. (So arguably, there's nothing to actually be confused about: both the alias and the original type are the "same thing".)

值得注意的是,在您特别为其他类别名的情况下,别名和原始类在运行时几乎无法区分:它们都是引用相同底层类型对象的变量。 (可以说,实际上没有什么可以混淆的:别名和原始类型都是“相同的东西”。)

Consequently, there's been very little incentive to find a naming convention for distinguishing between types and type aliases -- it doesn't really end up being an issue in practice.

因此,很难找到一种区分类型和类型别名的命名约定 - 它实际上并不是真正的问题。


That said, there are some implicit conventions for when to use type aliases. For example, people usually don't alias classes directly: type aliases, as Ethan said, are mainly meant to be used to help simplify larger compound types that are repeated in multiple places.

也就是说,何时使用类型别名有一些隐含的约定。例如,人们通常不直接使用别名:类型别名,如Ethan所说,主要用于帮助简化在多个地方重复的较大化合物类型。

This means that type aliases are mainly used when you would otherwise have a bunch of really ugly type signatures. It then becomes a "tradeoff" sort of thing: the reader does need to spend time looking up the definition of an alias and cache it in their head, but once they do, reading the rest of the code ought to be much more pleasant.

这意味着类型别名主要用于否则会有一堆非常难看的类型签名。然后它成为一种“权衡”的东西:读者确实需要花时间查找别名的定义并将其缓存在他们的头脑中,但是一旦他们这样做,阅读其余的代码应该更加愉快。

So if you still find type aliases potentially confusing and prefer to avoid using them unless necessary, that's perfectly fine: that's what pretty much everybody else does anyways. The only difference comes down to when you cross that tipping point from "unnecessary" to "necessary".

因此,如果您仍然发现类型别名可能会造成混淆,并且除非必要,我们更愿意避免使用它们,这非常好:这就是其他所有人都做的事情。唯一的区别在于你从“不必要”到“必要”的那个转折点。

#2


1  

There is no official convention for distinguishing the two.

没有官方公约来区分这两者。

Type aliases are very similar to typedefs in other languages. However, by convention type aliases are used for complex combinations of types, such as Message = Union[str, MyClass1, MyClass2, OtherClass], where writing out the full union multiple times would be cumbersome. So a single name annotation should either be: a type, or some combination of types that are aliased. You must go to the definition of that name to tell them apart.

类型别名与其他语言中的typedef非常相似。但是,按惯例,类型别名用于复杂的类型组合,例如Message = Union [str,MyClass1,MyClass2,OtherClass],其中多次写出完整联合将是麻烦的。因此,单个名称注释应该是:类型或别名的某些类型组合。您必须转到该名称的定义以区分它们。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/05/06/a82bbf0d76fe87d17bd7f90ad0fcd710.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com