简体   繁体   English

使用Python类型提示指定“ any”类型

[英]Specifying “any” type using Python type hints

We have a method in our Python 3.5 application where one of the in-parameters ( new_value in the example below) can be of any type, and we're wondering if there is a type hint that we can use for this case? 我们在Python 3.5应用程序中有一个方法,其中参数中的一个(下例中的new_value )可以是任何类型,并且我们想知道是否存在可以用于这种情况的类型提示?

def update(self, col_name: str, new_value) -> None:

(We would like the type hint for documentation purposes, to make the code easier to read) (我们希望出于文档目的使用类型提示,以使代码更易于阅读)

Grateful for help! 感谢帮助!

Depending on what exactly you want to use, there are two different options: 根据您要使用的确切内容,有两种不同的选择:

  1. If you want to basically opt-out of type-checking any_value completely and indicate it could be literally any type with zero restrictions, use typing.Any . 如果您希望基本上完全退出类型检查any_value并表明它实际上可以是任何具有零限制的类型,请使用typing.Any Example: 例:

     from typing import Any class MyThing: def update(self, col_name: str, new_value: Any) -> None: # Note: this typechecks; new_value can be anything, and # that object might have a foo method new_value.foo() # ...snip... 
  2. If you want to indicate that new_value can be of any type, but also want to ensure that the update method only ever uses new_value in a fully typesafe way, I would use object , which is the base type of every type in Python: 如果要指示new_value可以是任何类型,但也要确保update方法仅以完全类型安全的方式使用new_value ,则可以使用object ,它是Python中每种类型的基本类型:

     class MyThing: def update(self, col_name: str, new_value: object) -> None: # Note: this does not typecheck since new_value is of # type 'object', and 'object' is not guaranteed to have # a method named 'foo' new_value.foo() # ...snip... 

I personally bias towards using object -- Any is designed specifically as a "bridge" that lets you mix the typed and untyped worlds within your program. 我个人偏向于使用objectAny被专门设计为“桥梁”,使您可以在程序中混合类型化和非类型化的世界。 I personally think it's easier to reason about code if you keep those two worlds fairly distinct (eg by having a fully typed "core" possibly with a bunch of untyped code wrapping it) instead of a method that's both statically and dynamically typed. 我个人认为,如果您使这两个世界保持明显不同(例如,通过使用完全类型化的“核心”(可能带有一堆未类型化的代码来封装)),而不是使用静态和动态类型化的方法,则对代码进行推理更容易。

Of course, it's not always possible to do this... 当然,并非总是可以这样做...

(We would like the type hint for documentation purposes, to make the code easier to read) (我们希望出于文档目的使用类型提示,以使代码更易于阅读)

As an aside/as a suggestion, if you're going to use type hints, I would also strongly recommend going all the way and actually type-check your code using tools like mypy as a part of your development process. 顺便提一句,如果您要使用类型提示,强烈建议您一路走好,并在开发过程中使用mypy等工具对代码进行类型检查。

Using type hints for documentation is great, but it can be very confusing if your code doesn't conform to the documentation. 在文档中使用类型提示很棒,但是如果您的代码不符合文档,可能会造成混乱。 Since we have tools to automatically typecheck our code using those type hints, we might as well (and gain that extra guarantee). 由于我们拥有使用这些类型提示自动对代码进行类型检查的工具,因此我们也可以这样做(并获得额外的保证)。

(Pragmatically, trying to typecheck your entire codebase all at once can lead to a lot of noise -- what you can try and do instead is progressively typecheck your codebase. For example, you can configure mypy to typecheck only a set list of files (that you maybe grow over time?), make certain checks stricter or looser (perhaps on a per-file basis), and so forth. This works well with the "typed core, untyped wrapper" strategy mentioned above.) (实用上,一次尝试对整个代码库进行类型检查会导致很大的噪音-您可以尝试做的是逐步对代码库进行类型检查。例如,您可以将mypy配置为仅对一组文件列表进行类型检查(可能会随着时间的推移而增长?),进行更严格或更松散的检查(也许基于每个文件),依此类推。这与上面提到的“类型化的核心,未类型化的包装器”策略很好地配合。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM