简体   繁体   English

类型提示预期布尔值且始终为空列表

[英]Type hint expected boolean and always empty list

I have a list that I only need to check if it's empty or not.我有一个列表,我只需要检查它是否为空。

check = [] and True
print(check) # prints []
check = [1] and True
print(check) # prints True

I only want to use check in an if statement to check the truthiness of the variable, like so:我只想在if语句中使用check来检查变量的真实性,如下所示:

if check:
    print('Passed')

Should I type hint it like this?我应该像这样输入提示吗?

from typing import Literal, Union
check: Union[Literal[True], Literal[[]]] = [] and True

I don't even know if this how to annotate an always empty list, but the annotation itself seems silly, because to the reader, it only matters about the truthiness of it.我什至不知道这是否如何注释一个总是空的列表,但注释本身似乎很愚蠢,因为对读者来说,它只关心它的真实性。 Ideally I would just want check: bool = [] and True , but the statement doesn't always return a bool so it seems incorrect.理想情况下,我只想check: bool = [] and True ,但该语句并不总是返回bool所以它似乎不正确。

There is also this option to turn it into a bool :还有这个选项可以把它变成bool

check: bool = not not [] and True

or要么

check: bool =  bool([]) and True

but these seem like extra work for no reason, since I just need the truthiness and that is acquired regardless of whether I get an empty list or False .但这些似乎是无缘无故的额外工作,因为我只需要真实性,无论我得到的是空列表还是False可以获得真实性。

So, what is the proper way to annotate that a statement is to be interpreted only in a boolean context, if itself does not return a bool?那么,如果语句本身不返回布尔值,那么注释仅在布尔上下文中解释语句的正确方法是什么? Do I just convert it to be boolean and annotate with bool , or do I use something like Union[Literal[True], Literal[[]]] ?我只是将它转换为布尔值并使用bool注释,还是使用类似Union[Literal[True], Literal[[]]] And a side note, I don't know if Literal[[]] is even the proper way to annotate an always empty list (or if it's possible at all).附带说明一下,我不知道Literal[[]]是否是注释始终为空的列表的正确方法(或者是否可能)。

I think the correct way to annotate it is to use Union[List, bool] but Type Hints would'nt do data validation to check if the list is empty - for that take a look at PyDantic explored in this question我认为注释它的正确方法是使用Union[List, bool]但 Type Hints 不会进行数据验证来检查列表是否为空 - 为此看看 PyDantic 在这个问题中探索

from typing import List, Union


def check_list(my_list: Union[List, bool]):
    if my_list:
        print("got truth")
    else:
        print("got false")

Core Python allows for dynamic typing.核心 Python 允许动态类型。 If you know that you only want to use check in a boolean context annotate it with bool and use and True .如果您知道只想在布尔上下文中使用check ,请使用 bool 和 use and True对其进行注释。

check: bool
check = [] and True    # will contain []
...
check = [1] and True   # will contain True

This gives no warnings when I test it in PyCharm.当我在 PyCharm 中测试它时,这不会发出警告。

You can also use:您还可以使用:

check: bool = [] and True    # will contain []
...
check: bool = [1] and True   # will contain True

which gives no warning either...这也没有警告......

This almost feels like an inherently bad candidate for type-checking, to me.对我来说,这几乎感觉像是类型检查的固有不良候选人。 In this specific situation, you don't actually care about the type of the value returned, so long as you can call bool on it to test for its truthiness.在这种特定情况下,您实际上并不关心返回值的类型,只要您可以对其调用bool来测试其真实性即可。 Unless you're working with pandas / numpy or similar, there are very few python objects you can't call bool on — it doesn't require the presence of the __bool__ method or anything — so you can't use the usual solution for duck-typing, which would be to use typing.Protocol .除非你正在使用pandas / numpy或类似的东西,否则你不能调用bool Python 对象很少——它不需要__bool__方法或任何东西的存在——所以你不能使用通常的解决方案鸭子打字,这将使用typing.Protocol (See, for example, the SupportsInt and SupportsRound classes in the typing module, both of which test for the presence of certain methods.) (例如,请参见typing模块中的SupportsIntSupportsRound类,它们都测试某些方法的存在。)

As a result, I'd argue a hint of typing.Any would be just as informative and correct as a more elaborate one like typing.Union[list, bool] .因此,我会争辩说typing.Any的提示信息和更详细的信息一样typing.Union[list, bool]例如typing.Union[list, bool] The extra information in the second one is entirely redundant for your purposes, it would seem to me.在我看来,第二个中的额外信息对于您的目的来说是完全多余的。

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

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