"error: 'type' expects no type arguments, but 1 given" if type is aliased to another name
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 48/100
Hướng nghiên cứu
Bắt đầu bằng cách chạy reproducer Python 3.9 đã được báo cáo trong generictype.py với mypy và xác nhận lỗi dành riêng cho alias. Theo dõi cách mypy xử lý các alias của các built-in generic, sau đó thêm một regression test bao phủ Type = type và xác minh rằng việc kiểm tra thành công mà không làm hỏng hành vi hiện có của list và dict.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug Report
With the implementation of PEP 585 in Python 3.9, type now accepts a generic parameter. This makes typing.Type obsolete, reflected in its deprecation.
However, if the type built-in is assigned to another name and then used in a type declaration, mypy produces an error.
This does not occur with other types affected by PEP 585, or at least not all of them. I can confirm list and dict do not exhibit this behavior.
To Reproduce
Example code file:
generictype.py
Type = type
class MyInt(int):
pass
x_type: Type[int] = MyInt
- Install mypy into a Python 3.9 environment.
- Execute
mypyagainst the script:mypy .\generictype.py. No arguments or configuration are required.
Expected Behavior
This should result in a success run with no errors:
> mypy .\generictype.py
Success: no issues found in 1 source file
Actual Behavior
mypy identifies an error:
> mypy .\generictype.py
generictype.py:3: error: "type" expects no type arguments, but 1 given
Found 1 error in 1 file (checked 1 source file)
Also note that using x_type: type[int] = MyInt directly eliminates the error.
Your Environment
- Mypy version used:
mypy 0.800 - Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used:
Python 3.9.1(inside a venv) - Operating system and version: Windows 10
Motivation
My actual code doesn't look exactly like the reproducing code above. I encountered it trying to make my code backwards compatible with Python 3.7+. My real usage looks more like this:
from __future__ import annotations
if sys.version_info >= (3, 9):
List = list
Type = type
else:
# Legacy generic type annotation classes
# Deprecated in Python 3.9
# Remove once we no longer support Python 3.8 or lower
from typing import List
from typing import Type
class MyCustomContext:
data: List[int]
def __init__(self):
self.data = []
def __enter__(self) -> MyCustomContext:
return self
def add(i: int) -> None:
self.data.append(i)
def __exit__(
self,
ex_type: Optional[Type[Exception]],
ex_value: Optional[Exception],
ex_traceback: Optional[TracebackType],
) -> None:
if ex_value is None and len(self.data) > 100:
raise ValueError('Too much data')
For the time being, I can work around this by just continuing to use typing.Type, but I'd like to eliminate this deprecated class.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Merge trung bình
- 1 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 54
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/mypy
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
bug topic-configuration topic-error-reporting
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Issue tương tự
-
link-check link-check:sphinx-theme
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100