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