Skip to content

Commit 6b0c8df

Browse files
authored
Upgrade pyright, improve pyright config files (#8072)
1 parent a2ef476 commit 6b0c8df

File tree

12 files changed

+59
-29
lines changed

12 files changed

+59
-29
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,22 @@ jobs:
7777
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
7878
fail-fast: false
7979
env:
80-
PYRIGHT_VERSION: 1.1.248 # Must match pyright_test.py.
80+
PYRIGHT_VERSION: 1.1.254 # Must match pyright_test.py.
8181
steps:
8282
- uses: actions/checkout@v2
8383
- uses: jakebailey/pyright-action@v1
8484
with:
8585
version: ${{ env.PYRIGHT_VERSION }}
8686
python-platform: ${{ matrix.python-platform }}
8787
python-version: ${{ matrix.python-version }}
88-
no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
88+
no-comments: ${{ matrix.python-version != '3.10' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
8989
project: ./pyrightconfig.stricter.json
9090
- uses: jakebailey/pyright-action@v1
9191
with:
9292
version: ${{ env.PYRIGHT_VERSION }}
9393
python-platform: ${{ matrix.python-platform }}
9494
python-version: ${{ matrix.python-version }}
95-
no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
95+
no-comments: ${{ matrix.python-version != '3.10' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
9696

9797
stubtest-third-party:
9898
name: Check third party stubs with stubtest

pyrightconfig.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"strictListInference": true,
1313
"strictDictionaryInference": true,
1414
"strictParameterNoneValue": true,
15+
"strictSetInference": true,
1516
"reportFunctionMemberAccess": "error",
1617
"reportMissingModuleSource": "none",
1718
"reportMissingTypeStubs": "error",
@@ -40,16 +41,18 @@
4041
"reportUnboundVariable": "error",
4142
"reportInvalidStubStatement": "error",
4243
"reportInvalidTypeVarUse": "error",
43-
"reportPropertyTypeMismatch": "none",
4444
"reportSelfClsParameterName": "error",
4545
"reportUnsupportedDunderAll": "error",
46-
// Incompatible overrides are out of typeshed's control as they are
47-
// inherited from the implementation.
46+
"reportInconsistentConstructor": "error",
47+
"reportTypeCommentUsage": "error",
48+
"reportUnnecessaryComparison": "error",
49+
// Incompatible overrides and property type mismatches are out of typeshed's control
50+
// as they are inherited from the implementation.
51+
"reportPropertyTypeMismatch": "none",
4852
"reportIncompatibleMethodOverride": "none",
4953
"reportIncompatibleVariableOverride": "none",
50-
// Overlapping overloads cannot be enabled at this time because
51-
// of the "fractions.Fraction.__pow__" method and "tasks.gather" function.
52-
// Mypy's overlapping overload logic misses these issues (see mypy
53-
// issue #10143 and #10157).
54+
// Overlapping overloads are often necessary in a stub, meaning pyright's check
55+
// (which is stricter than mypy's; see mypy issue #10143 and #10157)
56+
// would cause many false positives and catch few bugs.
5457
"reportOverlappingOverload": "none",
5558
}

pyrightconfig.stricter.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"strictListInference": true,
8080
"strictDictionaryInference": true,
8181
"strictParameterNoneValue": true,
82+
"strictSetInference": true,
8283
"reportFunctionMemberAccess": "error",
8384
"reportMissingModuleSource": "none",
8485
"reportMissingTypeStubs": "error",
@@ -110,16 +111,18 @@
110111
"reportUnboundVariable": "error",
111112
"reportInvalidStubStatement": "error",
112113
"reportInvalidTypeVarUse": "error",
113-
"reportPropertyTypeMismatch": "none",
114114
"reportSelfClsParameterName": "error",
115115
"reportUnsupportedDunderAll": "error",
116-
// Incompatible overrides are out of typeshed's control as they are
117-
// inherited from the implementation.
116+
"reportInconsistentConstructor": "error",
117+
"reportTypeCommentUsage": "error",
118+
"reportUnnecessaryComparison": "error",
119+
// Incompatible overrides and property type mismatches are out of typeshed's control
120+
// as they are inherited from the implementation.
118121
"reportIncompatibleMethodOverride": "none",
119122
"reportIncompatibleVariableOverride": "none",
120-
// Overlapping overloads cannot be enabled at this time because
121-
// of the "fractions.Fraction.__pow__" method and "tasks.gather" function.
122-
// Mypy's overlapping overload logic misses these issues (see mypy
123-
// issue #10143 and #10157).
123+
"reportPropertyTypeMismatch": "none",
124+
// Overlapping overloads are often necessary in a stub, meaning pyright's check
125+
// (which is stricter than mypy's; see mypy issue #10143 and #10157)
126+
// would cause many false positives and catch few bugs.
124127
"reportOverlappingOverload": "none",
125128
}

stdlib/_weakref.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import Self
23
from collections.abc import Callable
34
from typing import Any, Generic, TypeVar, overload
45
from typing_extensions import final
@@ -20,7 +21,7 @@ class ProxyType(Generic[_T]): # "weakproxy"
2021

2122
class ReferenceType(Generic[_T]):
2223
__callback__: Callable[[ReferenceType[_T]], Any]
23-
def __init__(self, o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> None: ...
24+
def __new__(cls: type[Self], o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
2425
def __call__(self) -> _T | None: ...
2526
def __hash__(self) -> int: ...
2627
if sys.version_info >= (3, 9):

stdlib/abc.pyi

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import SupportsWrite
2+
from _typeshed import Self, SupportsWrite
33
from collections.abc import Callable
44
from typing import Any, Generic, TypeVar
55
from typing_extensions import Literal
@@ -11,7 +11,16 @@ _FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
1111
# These definitions have special processing in mypy
1212
class ABCMeta(type):
1313
__abstractmethods__: frozenset[str]
14-
def __init__(self, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> None: ...
14+
if sys.version_info >= (3, 11):
15+
def __new__(
16+
__mcls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
17+
) -> Self: ...
18+
else:
19+
# pyright doesn't like the first parameter being called mcls, hence the `pyright: ignore`
20+
def __new__(
21+
mcls: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any # pyright: ignore
22+
) -> Self: ...
23+
1524
def __instancecheck__(cls: ABCMeta, instance: Any) -> Any: ...
1625
def __subclasscheck__(cls: ABCMeta, subclass: Any) -> Any: ...
1726
def _dump_registry(cls: ABCMeta, file: SupportsWrite[str] | None = ...) -> None: ...

stdlib/ctypes/__init__.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ...
190190

191191
class _SimpleCData(Generic[_T], _CData):
192192
value: _T
193-
def __init__(self, value: _T = ...) -> None: ...
193+
# The TypeVar can be unsolved here,
194+
# but we can't use overloads without creating many, many mypy false-positive errors
195+
def __init__(self, value: _T = ...) -> None: ... # type: ignore
194196

195197
class c_byte(_SimpleCData[int]): ...
196198

stdlib/subprocess.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class CompletedProcess(Generic[_T]):
9595
# and writing all the overloads would be horrific.
9696
stdout: _T
9797
stderr: _T
98-
def __init__(self, args: _CMD, returncode: int, stdout: _T | None = ..., stderr: _T | None = ...) -> None: ...
98+
# type ignore on __init__ because the TypeVar can technically be unsolved, but see comment above
99+
def __init__(self, args: _CMD, returncode: int, stdout: _T | None = ..., stderr: _T | None = ...) -> None: ... # type: ignore
99100
def check_returncode(self) -> None: ...
100101
if sys.version_info >= (3, 9):
101102
def __class_getitem__(cls, item: Any) -> GenericAlias: ...

stubs/SQLAlchemy/@tests/stubtest_allowlist.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ sqlalchemy.engine.url.URL.normalized_query
125125
# runtime has extra internal arguments that are inconsistent across micro versions
126126
sqlalchemy.testing.engines.testing_engine
127127

128+
# __new__ signature conflicts with __init__ signature (which is more precise),
129+
# so __new__ is deliberately omitted in the stub
130+
sqlalchemy.sql.annotation.Annotated.__new__
131+
132+
# At runtime __new__ is defined, but we define __init__ in the stub
133+
# because otherwise all subclasses would be identified by pyright
134+
# as having conflicting __new__/__init__ methods
135+
sqlalchemy.orm.unitofwork.PostSortRec.__new__
136+
128137
# unclear problems
129138
sqlalchemy.sql.elements.quoted_name.lower
130139
sqlalchemy.sql.elements.quoted_name.upper

stubs/SQLAlchemy/sqlalchemy/orm/unitofwork.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ class Preprocess(IterateMappersMixin):
4949

5050
class PostSortRec:
5151
disabled: Any
52-
def __new__(cls, uow, *args): ...
52+
# At runtime __new__ is defined, not __init__,
53+
# But if we define __new__ here then all subclasses are identified by pyright
54+
# as having __init__ methods that are inconsistent with their __new__ methods
55+
def __init__(self, uow, *args) -> None: ...
5356
def execute_aggregate(self, uow, recs) -> None: ...
5457

5558
class ProcessAll(IterateMappersMixin, PostSortRec):

stubs/SQLAlchemy/sqlalchemy/sql/annotation.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class SupportsCloneAnnotations(SupportsAnnotations): ...
77
class SupportsWrappingAnnotations(SupportsAnnotations): ...
88

99
class Annotated:
10-
def __new__(cls, *args): ...
1110
__dict__: Any
1211
def __init__(self, element, values) -> None: ...
1312
def __reduce__(self): ...

0 commit comments

Comments
 (0)