Using precise code for pyright: ignore and re-enabling various pyright tests (#12576)

This commit is contained in:
Avasam
2024-08-21 21:34:52 -04:00
committed by GitHub
parent 2404a70f2b
commit 3719f02dbf
18 changed files with 60 additions and 75 deletions

View File

@@ -58,10 +58,13 @@ if sys.version_info >= (3, 9):
assert_type(os.environ | c, dict[str, str])
assert_type(e | c, dict[str, str])
# store "untainted" `CustomMappingWithDunderOr[str, str]` to test `__ior__` against ` dict[str, str]` later
# Invalid `e |= a` causes pyright to join `Unknown` to `e`'s type
f = e
e |= c
e |= a # type: ignore
# TODO: this test passes mypy, but fails pyright for some reason:
# c |= e
c |= f
c |= a # type: ignore

View File

@@ -7,6 +7,7 @@ from typing_extensions import assert_type
# mypy and pyright have different opinions about this one:
# mypy raises: 'Need type annotation for "bad"'
# pyright is fine with it.
# https://github.com/python/mypy/issues/12358
# bad = dict()
good: dict[str, str] = dict()
assert_type(good, Dict[str, str])

View File

@@ -17,10 +17,11 @@ assert_type(pow(1, 0, None), Literal[1])
# assert_type(pow(2, 4, 0), NoReturn)
assert_type(pow(2, 4), int)
# pyright infers a literal type here, but mypy does not. Unfortunately,
# there is no way to ignore an error only for mypy, so we can't check
# pyright's handling (https://github.com/python/mypy/issues/12358).
assert_type(2**4, int) # pyright: ignore
# pyright infers a literal type here, but mypy does not.
# Unfortunately, there is no way to ignore an error only for mypy,
# whilst getting both pyright and mypy to respect `type: ignore`.
# So we can't check pyright's handling (https://github.com/python/mypy/issues/12358).
assert_type(2**4, int) # pyright: ignore[reportAssertTypeFailure]
# pyright version: assert_type(2**4, Literal[16])
assert_type(pow(4, 6, None), int)
@@ -34,8 +35,7 @@ assert_type(pow(2, 8.5), float)
assert_type(2**8.6, float)
assert_type(pow(2, 8.6, None), float)
# TODO: Why does this pass pyright but not mypy??
# assert_type((-2) ** 0.5, complex)
assert_type((-2) ** 0.5, complex)
assert_type(pow((-5), 8.42, None), complex)

View File

@@ -38,7 +38,7 @@ assert_type(sum([Baz(), Baz()]), Union[Baz, Literal[0]])
# mypy and pyright infer the types differently for these, so we can't use assert_type
# Just test that no error is emitted for any of these
sum([("foo",), ("bar", "baz")], ()) # mypy: `tuple[str, ...]`; pyright: `tuple[()] | tuple[str] | tuple[str, str]`
sum([("foo",), ("bar", "baz")], ()) # mypy: `tuple[str, ...]`; pyright: `tuple[str] | tuple[str, str] | tuple[()]`
sum([5.6, 3.2]) # mypy: `float`; pyright: `float | Literal[0]`
sum([2.5, 5.8], 5) # mypy: `float`; pyright: `float | int`
@@ -52,4 +52,4 @@ sum([Bar(), Bar()]) # type: ignore
# TODO: these pass pyright with the current stubs, but mypy erroneously emits an error:
# sum([3, Fraction(7, 22), complex(8, 0), 9.83])
# sum([3, Decimal('0.98')])
# sum([3, Decimal("0.98")])

View File

@@ -15,14 +15,9 @@ class Foo:
assert_type(dc.fields(Foo), Tuple[dc.Field[Any], ...])
# Mypy correctly emits errors on these
# due to the fact it's a dataclass class, not an instance.
# Pyright, however, handles ClassVar members in protocols differently.
# See https://github.com/microsoft/pyright/issues/4339
#
# dc.asdict(Foo)
# dc.astuple(Foo)
# dc.replace(Foo)
dc.asdict(Foo) # type: ignore
dc.astuple(Foo) # type: ignore
dc.replace(Foo) # type: ignore
# See #9723 for why we can't make this assertion
# if dc.is_dataclass(Foo):
@@ -57,7 +52,7 @@ def is_dataclass_type(arg: type) -> None:
def check_other_isdataclass_overloads(x: type, y: object) -> None:
# TODO: pyright correctly emits an error on this, but mypy does not -- why?
# TODO: neither pyright nor mypy emit error on this -- why?
# dc.fields(x)
dc.fields(y) # type: ignore
@@ -75,27 +70,17 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None:
assert_type(x, Type["DataclassInstance"])
assert_type(dc.fields(x), Tuple[dc.Field[Any], ...])
# Mypy correctly emits an error on these due to the fact
# that it's a dataclass class, not a dataclass instance.
# Pyright, however, handles ClassVar members in protocols differently.
# See https://github.com/microsoft/pyright/issues/4339
#
# dc.asdict(x)
# dc.astuple(x)
# dc.replace(x)
dc.asdict(x) # type: ignore
dc.astuple(x) # type: ignore
dc.replace(x) # type: ignore
if dc.is_dataclass(y):
assert_type(y, Union["DataclassInstance", Type["DataclassInstance"]])
assert_type(dc.fields(y), Tuple[dc.Field[Any], ...])
# Mypy correctly emits an error on these due to the fact we don't know
# whether it's a dataclass class or a dataclass instance.
# Pyright, however, handles ClassVar members in protocols differently.
# See https://github.com/microsoft/pyright/issues/4339
#
# dc.asdict(y)
# dc.astuple(y)
# dc.replace(y)
dc.asdict(y) # type: ignore
dc.astuple(y) # type: ignore
dc.replace(y) # type: ignore
if dc.is_dataclass(y) and not isinstance(y, type):
assert_type(y, "DataclassInstance")

View File

@@ -60,10 +60,13 @@ if sys.version_info >= (3, 9):
assert_type(os.environ | c, dict[str, str])
assert_type(e | c, dict[str, str])
# store "untainted" `CustomMappingWithDunderOr[str, str]` to test `__ior__` against ` defaultdict[str, str]` later
# Invalid `e |= a` causes pyright to join `Unknown` to `e`'s type
f = e
e |= c
e |= a # type: ignore
# TODO: this test passes mypy, but fails pyright for some reason:
# c |= e
c |= f
c |= a # type: ignore

View File

@@ -345,15 +345,15 @@ class _OrderedDictValuesView(ValuesView[_VT_co], Reversible[_VT_co]):
# but they are not exposed anywhere)
# pyright doesn't have a specific error code for subclassing error!
@final
class _odict_keys(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] # pyright: ignore
class _odict_keys(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
def __reversed__(self) -> Iterator[_KT_co]: ...
@final
class _odict_items(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] # pyright: ignore
class _odict_items(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...
@final
class _odict_values(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] # pyright: ignore
class _odict_values(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
def __reversed__(self) -> Iterator[_VT_co]: ...
class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):

View File

@@ -229,18 +229,17 @@ if sys.version_info >= (3, 9):
else:
class _InitVarMeta(type):
# Not used, instead `InitVar.__class_getitem__` is called.
# pyright ignore is needed because pyright (not unreasonably) thinks this
# is an invalid use of InitVar.
def __getitem__(self, params: Any) -> InitVar[Any]: ... # pyright: ignore
# pyright (not unreasonably) thinks this is an invalid use of InitVar.
def __getitem__(self, params: Any) -> InitVar[Any]: ... # pyright: ignore[reportInvalidTypeForm]
class InitVar(Generic[_T], metaclass=_InitVarMeta):
type: Type[_T]
def __init__(self, type: Type[_T]) -> None: ...
if sys.version_info >= (3, 9):
@overload
def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ... # pyright: ignore
def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ... # pyright: ignore[reportInvalidTypeForm]
@overload
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... # pyright: ignore
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... # pyright: ignore[reportInvalidTypeForm]
if sys.version_info >= (3, 12):
def make_dataclass(

View File

@@ -2,7 +2,7 @@
# ruff: noqa: F811
# TODO: The collections import is required, otherwise mypy crashes.
# https://github.com/python/mypy/issues/16744
import collections # noqa: F401 # pyright: ignore
import collections # noqa: F401 # pyright: ignore[reportUnusedImport]
import sys
import typing_extensions
from _collections_abc import dict_items, dict_keys, dict_values