这是我的代码。有用。注意A线和B线。
from dataclasses import dataclass, field, InitVar
import datetime
from typing import List, Optional
@dataclass
class Book:
author: str = 'no data'
title: str = 'no data'
@dataclass
class Library:
name: str = 'no data'
books: List[Book] = field(default_factory=list) # line A - books attribute type annotation
gen_time: InitVar[bool] = True
creation_timestamp: Optional[str] = None
def __post_init__(self, gen_time):
if gen_time:
self.creation_timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def main():
b_1 = Book('J. Mouse', 'Beautiful mice')
b_2 = Book('M. Catling', 'Magic kittens')
b_3 = Book('A. Dogg', 'Tasty Dogs')
print(b_1, b_2, b_3)
bb_1 = Library('My library', gen_time=False)
bb_1.books.extend([bb_1, b_2, b_3, 1]) # line B - add books and not books to newly created empty library
print(bb_1)
if __name__ == '__main__':
main()
A 行说books
attrbute 应该是一个包含Book
对象的列表。在 BI 行中添加Book
、Library
和。PyCharm 没有告诉我这是否是一个问题。int
books
但是,如果我稍微修改 A 行books: List[str] = field(default_factory=list)
(Book
<-> str
),那么 PyCharm 会给出以下非常有用的警告:
预期类型“Iterable[str]”(匹配泛型类型“Iterable[_T]”),改为“List[Union[Library, Book, int]]”
那么,为什么 PyCharm 检查在这两种情况下表现不同呢?如何使用原始代码查看警告?
PS Win10 / Python 3.7 / PyCharm Community Edition 2019.2,无需任何修改/插件。