Skip to content

Commit 3719f02

Browse files
authored
Using precise code for pyright: ignore and re-enabling various pyright tests (#12576)
1 parent 2404a70 commit 3719f02

File tree

18 files changed

+60
-75
lines changed

18 files changed

+60
-75
lines changed

stdlib/@tests/test_cases/builtins/check_dict-py39.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ def test_dict_dot_or(
5858
assert_type(os.environ | c, dict[str, str])
5959
assert_type(e | c, dict[str, str])
6060

61+
# store "untainted" `CustomMappingWithDunderOr[str, str]` to test `__ior__` against ` dict[str, str]` later
62+
# Invalid `e |= a` causes pyright to join `Unknown` to `e`'s type
63+
f = e
64+
6165
e |= c
6266
e |= a # type: ignore
6367

64-
# TODO: this test passes mypy, but fails pyright for some reason:
65-
# c |= e
68+
c |= f
6669

6770
c |= a # type: ignore

stdlib/@tests/test_cases/builtins/check_dict.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# mypy and pyright have different opinions about this one:
88
# mypy raises: 'Need type annotation for "bad"'
99
# pyright is fine with it.
10+
# https://github.com/python/mypy/issues/12358
1011
# bad = dict()
1112
good: dict[str, str] = dict()
1213
assert_type(good, Dict[str, str])

stdlib/@tests/test_cases/builtins/check_pow.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
# assert_type(pow(2, 4, 0), NoReturn)
1818

1919
assert_type(pow(2, 4), int)
20-
# pyright infers a literal type here, but mypy does not. Unfortunately,
21-
# there is no way to ignore an error only for mypy, so we can't check
22-
# pyright's handling (https://github.com/python/mypy/issues/12358).
23-
assert_type(2**4, int) # pyright: ignore
20+
# pyright infers a literal type here, but mypy does not.
21+
# Unfortunately, there is no way to ignore an error only for mypy,
22+
# whilst getting both pyright and mypy to respect `type: ignore`.
23+
# So we can't check pyright's handling (https://github.com/python/mypy/issues/12358).
24+
assert_type(2**4, int) # pyright: ignore[reportAssertTypeFailure]
2425
# pyright version: assert_type(2**4, Literal[16])
2526
assert_type(pow(4, 6, None), int)
2627

@@ -34,8 +35,7 @@
3435
assert_type(2**8.6, float)
3536
assert_type(pow(2, 8.6, None), float)
3637

37-
# TODO: Why does this pass pyright but not mypy??
38-
# assert_type((-2) ** 0.5, complex)
38+
assert_type((-2) ** 0.5, complex)
3939

4040
assert_type(pow((-5), 8.42, None), complex)
4141

stdlib/@tests/test_cases/builtins/check_sum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __radd__(self, other: Any) -> Baz:
3838

3939
# mypy and pyright infer the types differently for these, so we can't use assert_type
4040
# Just test that no error is emitted for any of these
41-
sum([("foo",), ("bar", "baz")], ()) # mypy: `tuple[str, ...]`; pyright: `tuple[()] | tuple[str] | tuple[str, str]`
41+
sum([("foo",), ("bar", "baz")], ()) # mypy: `tuple[str, ...]`; pyright: `tuple[str] | tuple[str, str] | tuple[()]`
4242
sum([5.6, 3.2]) # mypy: `float`; pyright: `float | Literal[0]`
4343
sum([2.5, 5.8], 5) # mypy: `float`; pyright: `float | int`
4444

@@ -52,4 +52,4 @@ def __radd__(self, other: Any) -> Baz:
5252

5353
# TODO: these pass pyright with the current stubs, but mypy erroneously emits an error:
5454
# sum([3, Fraction(7, 22), complex(8, 0), 9.83])
55-
# sum([3, Decimal('0.98')])
55+
# sum([3, Decimal("0.98")])

stdlib/@tests/test_cases/check_dataclasses.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@ class Foo:
1515

1616
assert_type(dc.fields(Foo), Tuple[dc.Field[Any], ...])
1717

18-
# Mypy correctly emits errors on these
19-
# due to the fact it's a dataclass class, not an instance.
20-
# Pyright, however, handles ClassVar members in protocols differently.
21-
# See https://github.com/microsoft/pyright/issues/4339
22-
#
23-
# dc.asdict(Foo)
24-
# dc.astuple(Foo)
25-
# dc.replace(Foo)
18+
dc.asdict(Foo) # type: ignore
19+
dc.astuple(Foo) # type: ignore
20+
dc.replace(Foo) # type: ignore
2621

2722
# See #9723 for why we can't make this assertion
2823
# if dc.is_dataclass(Foo):
@@ -57,7 +52,7 @@ def is_dataclass_type(arg: type) -> None:
5752

5853

5954
def check_other_isdataclass_overloads(x: type, y: object) -> None:
60-
# TODO: pyright correctly emits an error on this, but mypy does not -- why?
55+
# TODO: neither pyright nor mypy emit error on this -- why?
6156
# dc.fields(x)
6257

6358
dc.fields(y) # type: ignore
@@ -75,27 +70,17 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None:
7570
assert_type(x, Type["DataclassInstance"])
7671
assert_type(dc.fields(x), Tuple[dc.Field[Any], ...])
7772

78-
# Mypy correctly emits an error on these due to the fact
79-
# that it's a dataclass class, not a dataclass instance.
80-
# Pyright, however, handles ClassVar members in protocols differently.
81-
# See https://github.com/microsoft/pyright/issues/4339
82-
#
83-
# dc.asdict(x)
84-
# dc.astuple(x)
85-
# dc.replace(x)
73+
dc.asdict(x) # type: ignore
74+
dc.astuple(x) # type: ignore
75+
dc.replace(x) # type: ignore
8676

8777
if dc.is_dataclass(y):
8878
assert_type(y, Union["DataclassInstance", Type["DataclassInstance"]])
8979
assert_type(dc.fields(y), Tuple[dc.Field[Any], ...])
9080

91-
# Mypy correctly emits an error on these due to the fact we don't know
92-
# whether it's a dataclass class or a dataclass instance.
93-
# Pyright, however, handles ClassVar members in protocols differently.
94-
# See https://github.com/microsoft/pyright/issues/4339
95-
#
96-
# dc.asdict(y)
97-
# dc.astuple(y)
98-
# dc.replace(y)
81+
dc.asdict(y) # type: ignore
82+
dc.astuple(y) # type: ignore
83+
dc.replace(y) # type: ignore
9984

10085
if dc.is_dataclass(y) and not isinstance(y, type):
10186
assert_type(y, "DataclassInstance")

stdlib/@tests/test_cases/collections/check_defaultdict-py39.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ def test_defaultdict_dot_or(
6060
assert_type(os.environ | c, dict[str, str])
6161
assert_type(e | c, dict[str, str])
6262

63+
# store "untainted" `CustomMappingWithDunderOr[str, str]` to test `__ior__` against ` defaultdict[str, str]` later
64+
# Invalid `e |= a` causes pyright to join `Unknown` to `e`'s type
65+
f = e
66+
6367
e |= c
6468
e |= a # type: ignore
6569

66-
# TODO: this test passes mypy, but fails pyright for some reason:
67-
# c |= e
70+
c |= f
6871

6972
c |= a # type: ignore

stdlib/collections/__init__.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ class _OrderedDictValuesView(ValuesView[_VT_co], Reversible[_VT_co]):
345345
# but they are not exposed anywhere)
346346
# pyright doesn't have a specific error code for subclassing error!
347347
@final
348-
class _odict_keys(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] # pyright: ignore
348+
class _odict_keys(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
349349
def __reversed__(self) -> Iterator[_KT_co]: ...
350350

351351
@final
352-
class _odict_items(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] # pyright: ignore
352+
class _odict_items(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
353353
def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...
354354

355355
@final
356-
class _odict_values(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] # pyright: ignore
356+
class _odict_values(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
357357
def __reversed__(self) -> Iterator[_VT_co]: ...
358358

359359
class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):

stdlib/dataclasses.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,17 @@ if sys.version_info >= (3, 9):
229229
else:
230230
class _InitVarMeta(type):
231231
# Not used, instead `InitVar.__class_getitem__` is called.
232-
# pyright ignore is needed because pyright (not unreasonably) thinks this
233-
# is an invalid use of InitVar.
234-
def __getitem__(self, params: Any) -> InitVar[Any]: ... # pyright: ignore
232+
# pyright (not unreasonably) thinks this is an invalid use of InitVar.
233+
def __getitem__(self, params: Any) -> InitVar[Any]: ... # pyright: ignore[reportInvalidTypeForm]
235234

236235
class InitVar(Generic[_T], metaclass=_InitVarMeta):
237236
type: Type[_T]
238237
def __init__(self, type: Type[_T]) -> None: ...
239238
if sys.version_info >= (3, 9):
240239
@overload
241-
def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ... # pyright: ignore
240+
def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ... # pyright: ignore[reportInvalidTypeForm]
242241
@overload
243-
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... # pyright: ignore
242+
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... # pyright: ignore[reportInvalidTypeForm]
244243

245244
if sys.version_info >= (3, 12):
246245
def make_dataclass(

stdlib/typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# ruff: noqa: F811
33
# TODO: The collections import is required, otherwise mypy crashes.
44
# https://github.com/python/mypy/issues/16744
5-
import collections # noqa: F401 # pyright: ignore
5+
import collections # noqa: F401 # pyright: ignore[reportUnusedImport]
66
import sys
77
import typing_extensions
88
from _collections_abc import dict_items, dict_keys, dict_values

stubs/JACK-Client/jack/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from numpy.typing import NDArray
1212
# Actual type: _cffi_backend.__CDataOwn <cdata 'struct _jack_position *'>
1313
# This is not a real subclassing. Just ensuring type-checkers sees this type as compatible with _CDataBase
1414
# pyright has no error code for subclassing final
15-
class _JackPositionT(_CDataBase): # type: ignore[misc] # pyright: ignore
15+
class _JackPositionT(_CDataBase): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
1616
audio_frames_per_video_frame: float
1717
bar: int
1818
bar_start_tick: float
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Verify that ImageTK images are valid to pass to TK code.
22
from __future__ import annotations
33

4-
# The following tests don't work at the moment, due to pyright getting
4+
# The following tests don't work on pyright at the moment, due to it getting
55
# confused by the existence of these stubs and annotations in the actual
66
# Pillow package.
77
# https://github.com/python/typeshed/issues/11688
8+
# pyright: reportArgumentType=false
9+
import tkinter
810

9-
# import tkinter
11+
from PIL import ImageTk
1012

11-
# from PIL import ImageTk
13+
photo = ImageTk.PhotoImage()
14+
bitmap = ImageTk.BitmapImage()
1215

13-
# photo = ImageTk.PhotoImage()
14-
# bitmap = ImageTk.BitmapImage()
16+
tkinter.Label(image=photo)
17+
tkinter.Label(image=bitmap)
1518

16-
# tkinter.Label(image=photo)
17-
# tkinter.Label(image=bitmap)
18-
19-
# tkinter.Label().configure(image=photo)
20-
# tkinter.Label().configure(image=bitmap)
19+
tkinter.Label().configure(image=photo)
20+
tkinter.Label().configure(image=bitmap)

stubs/WebOb/@tests/test_cases/check_wsgify.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ def app(request: Request) -> str:
5959
assert_type(app, "wsgify[Request, []]")
6060
assert_type(app(env, start_response), "Iterable[bytes]")
6161
assert_type(app(request), _AnyResponse)
62-
# FIXME: For some reason pyright complains here with
63-
# mismatch: expected "wsgify[Request, ()]" but received "wsgify[Request, ()]"
64-
# can you spot the difference?
65-
# assert_type(app(application), "wsgify[Request, []]")
62+
assert_type(app(application), "wsgify[Request, []]")
6663
application = app(application)
6764

6865

@@ -78,13 +75,10 @@ def m_app(request: Request) -> str:
7875

7976

8077
application = m_app
81-
# FIXME: same weird pyright error where it complains about the types
82-
# being the same
83-
# assert_type(m_app, "wsgify[Request, [WSGIApplication]]")
78+
assert_type(m_app, "wsgify[Request, [WSGIApplication]]")
8479
assert_type(m_app(env, start_response), "Iterable[bytes]")
8580
assert_type(m_app(request), _AnyResponse)
86-
# FIXME: and also here
87-
# assert_type(m_app(application), "wsgify[Request, [WSGIApplication]]")
81+
assert_type(m_app(application), "wsgify[Request, [WSGIApplication]]")
8882
application = m_app(application)
8983

9084

@@ -109,13 +103,13 @@ def valid_request_app(request: Request) -> None:
109103

110104

111105
# but the opposite is not allowed
112-
@wsgify # type:ignore
106+
@wsgify # type: ignore
113107
def invalid_request_app(request: MyRequest) -> None:
114108
pass
115109

116110

117111
# we can't really make passing extra arguments directly work
118112
# otherwise we have to give up most of our type safety for
119113
# something that should only be used through wsgify.middleware
120-
wsgify(args=(1,)) # type:ignore
121-
wsgify(kwargs={"ips": ["127.0.0.1"]}) # type:ignore
114+
wsgify(args=(1,)) # type: ignore
115+
wsgify(kwargs={"ips": ["127.0.0.1"]}) # type: ignore

stubs/openpyxl/@tests/test_cases/check_base_descriptors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Needed until mypy issues are solved
1+
# Needed until mypy issues are solved or https://github.com/python/mypy/issues/12358
22
# pyright: reportUnnecessaryTypeIgnoreComment=false
33
from __future__ import annotations
44

stubs/openpyxl/@tests/test_cases/check_nested_descriptors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Needed until mypy issues are solved
1+
# Needed until mypy issues are solved or https://github.com/python/mypy/issues/12358
22
# pyright: reportUnnecessaryTypeIgnoreComment=false
33

44
# These tests are essentially a mirror of check_base_descriptors

stubs/pygit2/pygit2/_pygit2.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class Blob(Object):
351351
# This is not a real subclassing. Just ensuring type-checkers sees this type as compatible with _CDataBase
352352
# pyright has no error code for subclassing final
353353
@final
354-
class Branch(Reference): # type: ignore[misc] # pyright: ignore
354+
class Branch(Reference): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
355355
branch_name: str
356356
raw_branch_name: bytes
357357
remote_name: str

stubs/pygit2/pygit2/utils.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def strarray_to_strings(arr: _GitStrArray) -> list[str]: ...
1818
# Actual type: _cffi_backend.__CDataOwn <cdata 'struct git_strarray *'>
1919
# This is not a real subclassing. Just ensuring type-checkers sees this type as compatible with _CDataBase
2020
# pyright has no error code for subclassing final
21-
class _GitStrArray(_CDataBase): # type: ignore[misc] # pyright: ignore
21+
class _GitStrArray(_CDataBase): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
2222
count: int
2323
strings: _CDataBase # <cdata 'char * *'>
2424

stubs/pywin32/win32comext/axdebug/documents.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DebugDocumentProvider(gateways.DebugDocumentProvider):
1515

1616
# error: Cannot determine consistent method resolution order (MRO) for "DebugDocumentText"
1717
# pyright doesn't have a specific error code for MRO error!
18-
class DebugDocumentText(gateways.DebugDocumentInfo, gateways.DebugDocumentText, gateways.DebugDocument): # type: ignore[misc] # pyright: ignore
18+
class DebugDocumentText(gateways.DebugDocumentInfo, gateways.DebugDocumentText, gateways.DebugDocument): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
1919
codeContainer: Incomplete
2020
def __init__(self, codeContainer) -> None: ...
2121
def GetName(self, dnt): ...

tests/pytype_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def get_missing_modules(files_to_test: Sequence[str]) -> Iterable[str]:
198198
# Skips comments, empty lines, and stdlib files, which are in
199199
# the exclude list because pytype has its own version.
200200
continue
201-
unused_stubs_prefix, unused_pkg, mod_path = fi.split("/", 2) # pyright: ignore [reportUnusedVariable]
201+
unused_stubs_prefix, unused_pkg, mod_path = fi.split("/", 2) # pyright: ignore[reportUnusedVariable]
202202
missing_modules.add(os.path.splitext(mod_path)[0])
203203
return missing_modules
204204

0 commit comments

Comments
 (0)