Skip to content

Upgrade pyright, improve pyright config files #8072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1695d6a
Beef up pyright config
AlexWaygood Jun 14, 2022
99be1e6
Merge branch 'master' into pyright
AlexWaygood Jun 14, 2022
dfb48bd
Merge remote-tracking branch 'origin/master' into pyright
AlexWaygood Jun 14, 2022
91b9fa5
Merge branch 'master' into pyright
JelleZijlstra Jun 14, 2022
2b208cb
Add ignores for unfixable things
AlexWaygood Jun 14, 2022
c3839ce
Merge branch 'pyright' of https://github.com/AlexWaygood/typeshed int…
AlexWaygood Jun 14, 2022
8414ae4
Update stdlib/subprocess.pyi
AlexWaygood Jun 14, 2022
4dd0069
Update pyrightconfig.stricter.json
AlexWaygood Jun 14, 2022
1ed0f95
Change two `__init__` methods to `__new__` methods
AlexWaygood Jun 14, 2022
879d274
Merge branch 'pyright' of https://github.com/AlexWaygood/typeshed int…
AlexWaygood Jun 14, 2022
968e91d
Allowlists etc.
AlexWaygood Jun 14, 2022
7db0f00
Update pyrightconfig.json
AlexWaygood Jun 14, 2022
3567494
More allowlist shenanigans
AlexWaygood Jun 14, 2022
7feccdf
Merge branch 'pyright' of https://github.com/AlexWaygood/typeshed int…
AlexWaygood Jun 14, 2022
7c74d5a
stubtest
AlexWaygood Jun 14, 2022
2297de2
Merge branch 'master' into pyright
AlexWaygood Jun 15, 2022
b81c85c
Fix a constructor in `SQLAlchemy`
AlexWaygood Jun 16, 2022
067e087
Merge branch 'master' into pyright
AlexWaygood Jun 16, 2022
da1f2a1
Merge branch 'pyright' of https://github.com/AlexWaygood/typeshed int…
AlexWaygood Jun 16, 2022
b9c5197
White lies in SQLAlchemy
AlexWaygood Jun 16, 2022
2b5e5ed
Allow
AlexWaygood Jun 16, 2022
86edde9
Update stubs/SQLAlchemy/@tests/stubtest_allowlist.txt
AlexWaygood Jun 16, 2022
6b11e9c
Update stdlib/abc.pyi
AlexWaygood Jun 16, 2022
463bee3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 16, 2022
e014952
Undo the damage done by pre-commit
AlexWaygood Jun 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ jobs:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
fail-fast: false
env:
PYRIGHT_VERSION: 1.1.248 # Must match pyright_test.py.
PYRIGHT_VERSION: 1.1.254 # Must match pyright_test.py.
steps:
- uses: actions/checkout@v2
- uses: jakebailey/pyright-action@v1
with:
version: ${{ env.PYRIGHT_VERSION }}
python-platform: ${{ matrix.python-platform }}
python-version: ${{ matrix.python-version }}
no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
no-comments: ${{ matrix.python-version != '3.10' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
project: ./pyrightconfig.stricter.json
- uses: jakebailey/pyright-action@v1
with:
version: ${{ env.PYRIGHT_VERSION }}
python-platform: ${{ matrix.python-platform }}
python-version: ${{ matrix.python-version }}
no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
no-comments: ${{ matrix.python-version != '3.10' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.

stubtest-third-party:
name: Check third party stubs with stubtest
Expand Down
17 changes: 10 additions & 7 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"strictListInference": true,
"strictDictionaryInference": true,
"strictParameterNoneValue": true,
"strictSetInference": true,
"reportFunctionMemberAccess": "error",
"reportMissingModuleSource": "none",
"reportMissingTypeStubs": "error",
Expand Down Expand Up @@ -40,16 +41,18 @@
"reportUnboundVariable": "error",
"reportInvalidStubStatement": "error",
"reportInvalidTypeVarUse": "error",
"reportPropertyTypeMismatch": "none",
"reportSelfClsParameterName": "error",
"reportUnsupportedDunderAll": "error",
// Incompatible overrides are out of typeshed's control as they are
// inherited from the implementation.
"reportInconsistentConstructor": "error",
"reportTypeCommentUsage": "error",
"reportUnnecessaryComparison": "error",
// Incompatible overrides and property type mismatches are out of typeshed's control
// as they are inherited from the implementation.
"reportPropertyTypeMismatch": "none",
"reportIncompatibleMethodOverride": "none",
"reportIncompatibleVariableOverride": "none",
// Overlapping overloads cannot be enabled at this time because
// of the "fractions.Fraction.__pow__" method and "tasks.gather" function.
// Mypy's overlapping overload logic misses these issues (see mypy
// issue #10143 and #10157).
// Overlapping overloads are often necessary in a stub, meaning pyright's check
// (which is stricter than mypy's; see mypy issue #10143 and #10157)
// would cause many false positives and catch few bugs.
"reportOverlappingOverload": "none",
}
17 changes: 10 additions & 7 deletions pyrightconfig.stricter.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"strictListInference": true,
"strictDictionaryInference": true,
"strictParameterNoneValue": true,
"strictSetInference": true,
"reportFunctionMemberAccess": "error",
"reportMissingModuleSource": "none",
"reportMissingTypeStubs": "error",
Expand Down Expand Up @@ -110,16 +111,18 @@
"reportUnboundVariable": "error",
"reportInvalidStubStatement": "error",
"reportInvalidTypeVarUse": "error",
"reportPropertyTypeMismatch": "none",
"reportSelfClsParameterName": "error",
"reportUnsupportedDunderAll": "error",
// Incompatible overrides are out of typeshed's control as they are
// inherited from the implementation.
"reportInconsistentConstructor": "error",
"reportTypeCommentUsage": "error",
"reportUnnecessaryComparison": "error",
// Incompatible overrides and property type mismatches are out of typeshed's control
// as they are inherited from the implementation.
"reportIncompatibleMethodOverride": "none",
"reportIncompatibleVariableOverride": "none",
// Overlapping overloads cannot be enabled at this time because
// of the "fractions.Fraction.__pow__" method and "tasks.gather" function.
// Mypy's overlapping overload logic misses these issues (see mypy
// issue #10143 and #10157).
"reportPropertyTypeMismatch": "none",
// Overlapping overloads are often necessary in a stub, meaning pyright's check
// (which is stricter than mypy's; see mypy issue #10143 and #10157)
// would cause many false positives and catch few bugs.
"reportOverlappingOverload": "none",
}
3 changes: 2 additions & 1 deletion stdlib/_weakref.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from _typeshed import Self
from collections.abc import Callable
from typing import Any, Generic, TypeVar, overload
from typing_extensions import final
Expand All @@ -20,7 +21,7 @@ class ProxyType(Generic[_T]): # "weakproxy"

class ReferenceType(Generic[_T]):
__callback__: Callable[[ReferenceType[_T]], Any]
def __init__(self, o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> None: ...
def __new__(cls: type[Self], o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ...
def __call__(self) -> _T | None: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 9):
Expand Down
13 changes: 11 additions & 2 deletions stdlib/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import SupportsWrite
from _typeshed import Self, SupportsWrite
from collections.abc import Callable
from typing import Any, Generic, TypeVar
from typing_extensions import Literal
Expand All @@ -11,7 +11,16 @@ _FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
# These definitions have special processing in mypy
class ABCMeta(type):
__abstractmethods__: frozenset[str]
def __init__(self, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> None: ...
if sys.version_info >= (3, 11):
def __new__(
__mcls: type[Self], __name: str, __bases: tuple[type, ...], __namespace: dict[str, Any], **kwargs: Any
) -> Self: ...
else:
# pyright doesn't like the first parameter being called mcls, hence the `pyright: ignore`
def __new__(
mcls: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs: Any # pyright: ignore
) -> Self: ...

def __instancecheck__(cls: ABCMeta, instance: Any) -> Any: ...
def __subclasscheck__(cls: ABCMeta, subclass: Any) -> Any: ...
def _dump_registry(cls: ABCMeta, file: SupportsWrite[str] | None = ...) -> None: ...
Expand Down
4 changes: 3 additions & 1 deletion stdlib/ctypes/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ...

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

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

Expand Down
3 changes: 2 additions & 1 deletion stdlib/subprocess.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class CompletedProcess(Generic[_T]):
# and writing all the overloads would be horrific.
stdout: _T
stderr: _T
def __init__(self, args: _CMD, returncode: int, stdout: _T | None = ..., stderr: _T | None = ...) -> None: ...
# type ignore on __init__ because the TypeVar can technically be unsolved, but see comment above
def __init__(self, args: _CMD, returncode: int, stdout: _T | None = ..., stderr: _T | None = ...) -> None: ... # type: ignore
def check_returncode(self) -> None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
Expand Down
9 changes: 9 additions & 0 deletions stubs/SQLAlchemy/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ sqlalchemy.engine.url.URL.normalized_query
# runtime has extra internal arguments that are inconsistent across micro versions
sqlalchemy.testing.engines.testing_engine

# __new__ signature conflicts with __init__ signature (which is more precise),
# so __new__ is deliberately omitted in the stub
sqlalchemy.sql.annotation.Annotated.__new__

# At runtime __new__ is defined, but we define __init__ in the stub
# because otherwise all subclasses would be identified by pyright
# as having conflicting __new__/__init__ methods
sqlalchemy.orm.unitofwork.PostSortRec.__new__

# unclear problems
sqlalchemy.sql.elements.quoted_name.lower
sqlalchemy.sql.elements.quoted_name.upper
Expand Down
5 changes: 4 additions & 1 deletion stubs/SQLAlchemy/sqlalchemy/orm/unitofwork.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class Preprocess(IterateMappersMixin):

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

class ProcessAll(IterateMappersMixin, PostSortRec):
Expand Down
1 change: 0 additions & 1 deletion stubs/SQLAlchemy/sqlalchemy/sql/annotation.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class SupportsCloneAnnotations(SupportsAnnotations): ...
class SupportsWrappingAnnotations(SupportsAnnotations): ...

class Annotated:
def __new__(cls, *args): ...
__dict__: Any
def __init__(self, element, values) -> None: ...
def __reduce__(self): ...
Expand Down
2 changes: 1 addition & 1 deletion tests/pyright_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
from pathlib import Path

_PYRIGHT_VERSION = "1.1.248" # Must match .github/workflows/tests.yml.
_PYRIGHT_VERSION = "1.1.254" # Must match .github/workflows/tests.yml.
_WELL_KNOWN_FILE = Path("tests", "pyright_test.py")


Expand Down
8 changes: 4 additions & 4 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ _collections_abc.Set.__rxor__
_csv.Dialect.__init__ # C __init__ signature is inaccurate
_socket.*
_threading_local.local.__new__
_weakref.ref.__call__
abc.ABCMeta.__new__ # pytype wants the parameter named cls and not mcls
_weakref.ref.* # Alias for _weakref.ReferenceType, problems should be fixed there
_weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy
_weakref.ProxyType.__getattr__ # Should have all attributes of proxy
_weakref.ReferenceType.__call__ # C function default annotation is wrong
_weakref.ReferenceType.__init__ # Runtime defines __new__ but stubtest thinks __init__ is also defined.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a guess. But if I'm not correct here, I think the best thing to do here is to have a white lie in the stub anyway.

argparse.Namespace.__getattr__ # The whole point of this class is its attributes are dynamic
asynchat.async_chat.encoding # Removal planned for 3.12, can add if someone needs this
asynchat.async_chat.use_encoding # Removal planned for 3.12, can add if someone needs this
Expand Down Expand Up @@ -221,11 +221,11 @@ urllib.request.HTTPPasswordMgrWithPriorAuth.__init__ # Args are passed as is to
warnings.catch_warnings.__init__ # Defining this ruins the __new__ overrides
weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy
weakref.ProxyType.__getattr__ # Should have all attributes of proxy
weakref.ReferenceType.__call__ # C function default annotation is wrong
weakref.ReferenceType.* # Alias for _weakref.ReferenceType, problems should be fixed there
weakref.WeakKeyDictionary.get
weakref.WeakKeyDictionary.update
weakref.WeakValueDictionary.get
weakref.ref.__call__
weakref.ref.* # Alias for _weakref.ReferenceType, problems should be fixed there
webbrowser.UnixBrowser.remote_action # always overridden in inheriting class
webbrowser.UnixBrowser.remote_action_newtab # always overridden in inheriting class
webbrowser.UnixBrowser.remote_action_newwin # always overridden in inheriting class
Expand Down