python / python/typing

`type` and `disallow-any-generics`

Đang mở
#1,353 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

topic: other
Ngôn ngữ chính
Python
Star
1.8k
Fork
302
Merge trung bình
23 giờ
Pull request đã merge (30 ngày)
8

Mô tả

I was surprised (discovered in https://github.com/python/typeshed/issues/9723), that typecheckers currently do not consider type in annotation context purely generic alias: unlike list or dict, it is allowed to remain unsubscripted. This results in unexpected behaviour like the issue above.

mypy issue (open): https://github.com/python/mypy/issues/12301
pyright issue (closed "working as intended", with comment by Eric Traut I don't really understand - probably it's about inheritance from type): https://github.com/microsoft/pyright/issues/4658

I think that this behaviour should be changed. I do understand possible effect on existing codebases (backwards incompatibility), but this is a thing really easy to fix: all occurrences of bare type in annotations have to be replaced with type[object] (or type[Any] to match curent behaviour, but it's probably worse and should be rarely needed). See typeshed stats at the end.

In addition to issue above, here are some other things to consider:

  • typing.Type without type argument is reported as error. This means that type and typing.Type are not equivalent in annotations context, and thus PEP585 deprecation can be harmful. From now, I'll switch back to typing.Type to be sure that mypy will point out missing part.
  • This thing is somewhat hidden: I wasn't aware of such type special treatment, and it is not obvious.
  • This is not absolutely straightforward: while missing generic args should be marked in annotations, it should not happen in class base (so that class MyMeta(type) typechecks). I don't see any other places where parameter omission should be allowed.
  • PEP585 does not mention any special type treatment.
  • All use cases in annotations can be covered with type[object] or type[Any].

Typeshed stats

For a quick estimate, I just grepped the typeshed for type usage in annotations. Here's the result (with unrelated lines mostly removed):

grep -E '\btype\b[^\[]' . -rn --exclude-dir=venv --include='*.pyi' | grep -v -E 'type: |^.+:[[:digit:]]+:[[:space:]]*(#|""")|def type|@type|class|import' | grep -E '\btype\b' --color=always
./stubs/pywin32/pythoncom.pyi:391:TypeIIDs: dict[_win32typing.PyIID, type]
./stubs/pywin32/win32/odbc.pyi:9:_odbcError: TypeAlias = type  # noqa: Y042  # Does not exist at runtime, but odbc.odbcError is a valid type.
./stubs/pywin32/pythonwin/win32ui.pyi:369:types: dict[str, type]
./stubs/six/six/__init__.pyi:48:def create_unbound_method(func: types.FunctionType, cls: type) -> types.FunctionType: ...
./stubs/requests/requests/compat.pyi:23:basestring: tuple[type, ...]
./stubs/requests/requests/compat.pyi:24:numeric_types: tuple[type, ...]
./stubs/requests/requests/compat.pyi:25:integer_types: tuple[type, ...]
./stubs/D3DShot/d3dshot/dll/__init__.pyi:26:    def QueryInterface(self, interface: type, iid: _CData | None = ...) -> _HRESULT: ...
./stubs/python-xlib/Xlib/display.pyi:82:    def extension_add_event(self, code: int, evt: type, name: str | None = ...) -> None: ...
./stubs/pynput/pynput/_util.pyi:21:def prefix(base: type | tuple[type | tuple[Any, ...], ...], cls: type) -> str | None: ...
./stubs/pynput/pynput/_util.pyi:25:    _HANDLED_EXCEPTIONS: ClassVar[tuple[type | tuple[Any, ...], ...]]  # undocumented
./stubs/paramiko/paramiko/ssh_exception.pyi:38:    def __reduce__(self) -> tuple[type, tuple[Mapping[tuple[str, int] | tuple[str, int, int, int], Exception]]]: ...
./stubs/SQLAlchemy/sqlalchemy/log.pyi:5:_ClsT = TypeVar("_ClsT", bound=type)
./stubs/SQLAlchemy/sqlalchemy/util/compat.pyi:61:string_types: tuple[type, ...]
./stubs/SQLAlchemy/sqlalchemy/util/compat.pyi:62:binary_types: tuple[type, ...]
./stubs/SQLAlchemy/sqlalchemy/util/compat.pyi:65:int_types: tuple[type, ...]
./stubs/decorator/decorator.pyi:15:def get_init(cls: type) -> None: ...
./stubs/decorator/decorator.pyi:72:def append(a: type, vancestors: list[type]) -> None: ...
./stubs/pyasn1/pyasn1/type/namedtype.pyi:4:    def __init__(self, name, asn1Object, openType: type | None = ...) -> None: ...
./stubs/aiofiles/aiofiles/threadpool/utils.pyi:5:_T = TypeVar("_T", bound=type)
./stubs/fpdf2/fpdf/drawing.pyi:20:NumberClass: tuple[type, ...]
./stubs/Flask-SQLAlchemy/flask_sqlalchemy/model.pyi:8:def should_set_tablename(cls: type) -> bool: ...
./stubs/Flask-SQLAlchemy/flask_sqlalchemy/model.pyi:15:    def __init__(cls, name: str, bases: tuple[type, ...], d: dict[str, Any]) -> None: ...
./stubs/Flask-SQLAlchemy/flask_sqlalchemy/model.pyi:19:    def __init__(cls, name: str, bases: tuple[type, ...], d: dict[str, Any]) -> None: ...
./stdlib/pydoc.pyi:28:def allmethods(cl: type) -> MutableMapping[str, MethodType]: ...
./stdlib/pydoc.pyi:121:        self, tree: list[tuple[type, tuple[type, ...]] | list[Any]], modname: str, parent: type | None = None
./stdlib/pydoc.pyi:142:        cl: type | None = None,
./stdlib/pydoc.pyi:163:        self, tree: list[tuple[type, tuple[type, ...]] | list[Any]], modname: str, parent: type | None = None, prefix: str = ""
./stdlib/builtins.pyi:144:    __bases__: tuple[type, ...]
./stdlib/builtins.pyi:157:    def __mro__(self) -> tuple[type, ...]: ...
./stdlib/builtins.pyi:167:    def __init__(self, __name: str, __bases: tuple[type, ...], __dict: dict[str, Any], **kwds: Any) -> None: ...
./stdlib/builtins.pyi:172:        cls: type[_typeshed.Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwds: Any
./stdlib/builtins.pyi:178:    def mro(self) -> list[type]: ...
./stdlib/builtins.pyi:182:    def __prepare__(metacls, __name: str, __bases: tuple[type, ...], **kwds: Any) -> Mapping[str, object]: ...
./stdlib/builtins.pyi:1398:    _ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]
./stdlib/builtins.pyi:1400:    _ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...]
./stdlib/asyncio/futures.pyi:58:    def set_exception(self, __exception: type | BaseException) -> None: ...
./stdlib/types.pyi:563:    name: str, bases: tuple[type, ...] = ..., kwds: dict[str, Any] | None = None
./stdlib/types.pyi:564:) -> tuple[type, dict[str, Any], dict[str, Any]]: ...
./stdlib/types.pyi:591:        def __init__(self, origin: type, args: Any) -> None: ...
./stdlib/xmlrpc/server.pyi:114:        cl: type | None = None,
./stdlib/email/contentmanager.pyi:9:    def add_set_handler(self, typekey: type, handler: Callable[..., Any]) -> None: ...
./stdlib/inspect.pyi:466:def getmro(cls: type) -> tuple[type, ...]: ...
./stdlib/string.pyi:40:        def __init__(cls, name: str, bases: tuple[type, ...], dct: dict[str, Any]) -> None: ...
./stdlib/unittest/case.pyi:72:    _IsInstanceClassInfo: TypeAlias = type | UnionType | tuple[type | UnionType | tuple[Any, ...], ...]
./stdlib/unittest/case.pyi:74:    _IsInstanceClassInfo: TypeAlias = type | tuple[type | tuple[Any, ...], ...]
./stdlib/dis.pyi:38:_HaveCodeType: TypeAlias = types.MethodType | types.FunctionType | types.CodeType | type | Callable[..., Any]
./stdlib/pickle.pyi:155:    dispatch_table: Mapping[type, Callable[[Any], _ReducedType]]
./stdlib/pickle.pyi:157:    dispatch: ClassVar[dict[type, Callable[[Unpickler, Any], None]]]  # undocumented, _Pickler only
./stdlib/asyncore.pyi:69:def compact_traceback() -> tuple[tuple[str, str, str], type, type, str]: ...
./stdlib/tarfile.pyi:86:PAX_NUMBER_FIELDS: dict[str, type]
./stdlib/typing_extensions.pyi:235:            _field_types: collections.OrderedDict[str, type]
./stdlib/typing_extensions.pyi:237:            _field_types: dict[str, type]
./stdlib/enum.pyi:86:            bases: tuple[type, ...],
./stdlib/abc.pyi:17:            __mcls: type[_typeshed.Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
./stdlib/abc.pyi:21:            mcls: type[_typeshed.Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any
./stdlib/typing.pyi:777:        _field_types: collections.OrderedDict[str, type]
./stdlib/typing.pyi:779:        _field_types: dict[str, type]
./stdlib/functools.pyi:162:    fasttypes: set[type] = ...,
./stdlib/functools.pyi:163:    tuple: type = ...,

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc xem xét các issue mypy và pyright được liên kết, sau đó kiểm tra các vị trí xuất hiện .pyi được liệt kê trong stubs/ và stdlib/, đặc biệt là builtins.pyi. Xác định bare type nên khác nhau như thế nào trong các annotation so với các lớp cơ sở, và ghi lại hành vi đã thống nhất cùng phạm vi migration; issue được hoàn thành khi hành vi typing và các trường hợp bị ảnh hưởng có một cách giải quyết được maintainer phê duyệt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.