From 789c12ad90bea7ab2af9fbcf9b9ee410d6fed874 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 26 May 2022 23:32:56 -0700 Subject: [PATCH] constructors: Fix defaulted TypeVars (#7965) From the list in https://github.com/microsoft/pyright/issues/3501 --- stdlib/_weakrefset.pyi | 7 +++++-- stdlib/contextvars.pyi | 5 ++++- stdlib/graphlib.pyi | 7 +++++-- stubs/SQLAlchemy/sqlalchemy/util/_collections.pyi | 5 ++++- stubs/contextvars/contextvars.pyi | 7 +++++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/stdlib/_weakrefset.pyi b/stdlib/_weakrefset.pyi index 382dbdeb6..9e9269758 100644 --- a/stdlib/_weakrefset.pyi +++ b/stdlib/_weakrefset.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import Self from collections.abc import Iterable, Iterator, MutableSet -from typing import Any, Generic, TypeVar +from typing import Any, Generic, TypeVar, overload if sys.version_info >= (3, 9): from types import GenericAlias @@ -12,7 +12,10 @@ _S = TypeVar("_S") _T = TypeVar("_T") class WeakSet(MutableSet[_T], Generic[_T]): - def __init__(self, data: Iterable[_T] | None = ...) -> None: ... + @overload + def __init__(self, data: None = ...) -> None: ... + @overload + def __init__(self, data: Iterable[_T]) -> None: ... def add(self, item: _T) -> None: ... def clear(self) -> None: ... def discard(self, item: _T) -> None: ... diff --git a/stdlib/contextvars.pyi b/stdlib/contextvars.pyi index 341cd8491..266d96bce 100644 --- a/stdlib/contextvars.pyi +++ b/stdlib/contextvars.pyi @@ -14,7 +14,10 @@ _P = ParamSpec("_P") @final class ContextVar(Generic[_T]): - def __init__(self, name: str, *, default: _T = ...) -> None: ... + @overload + def __init__(self, name: str) -> None: ... + @overload + def __init__(self, name: str, *, default: _T) -> None: ... @property def name(self) -> str: ... @overload diff --git a/stdlib/graphlib.pyi b/stdlib/graphlib.pyi index 2fca402bf..4c6959dec 100644 --- a/stdlib/graphlib.pyi +++ b/stdlib/graphlib.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import SupportsItems from collections.abc import Iterable -from typing import Any, Generic, TypeVar +from typing import Any, Generic, TypeVar, overload __all__ = ["TopologicalSorter", "CycleError"] @@ -11,7 +11,10 @@ if sys.version_info >= (3, 11): from types import GenericAlias class TopologicalSorter(Generic[_T]): - def __init__(self, graph: SupportsItems[_T, Iterable[_T]] | None = ...) -> None: ... + @overload + def __init__(self, graph: None = ...) -> None: ... + @overload + def __init__(self, graph: SupportsItems[_T, Iterable[_T]]) -> None: ... def add(self, node: _T, *predecessors: _T) -> None: ... def prepare(self) -> None: ... def is_active(self) -> bool: ... diff --git a/stubs/SQLAlchemy/sqlalchemy/util/_collections.pyi b/stubs/SQLAlchemy/sqlalchemy/util/_collections.pyi index 06cd4378b..153b6952e 100644 --- a/stubs/SQLAlchemy/sqlalchemy/util/_collections.pyi +++ b/stubs/SQLAlchemy/sqlalchemy/util/_collections.pyi @@ -91,7 +91,10 @@ else: def sort_dictionary(d, key: Any | None = ...): ... class OrderedSet(set[_T], Generic[_T]): - def __init__(self, d: Iterable[_T] | None = ...) -> None: ... + @overload + def __init__(self, d: None = ...) -> None: ... + @overload + def __init__(self, d: Iterable[_T]) -> None: ... def add(self, element: _T) -> None: ... def remove(self, element: _T) -> None: ... def insert(self, pos: int, element: _T) -> None: ... diff --git a/stubs/contextvars/contextvars.pyi b/stubs/contextvars/contextvars.pyi index d363d7370..044d8a43c 100644 --- a/stubs/contextvars/contextvars.pyi +++ b/stubs/contextvars/contextvars.pyi @@ -1,6 +1,6 @@ import sys from collections.abc import Callable, Iterator, Mapping -from typing import Any, ClassVar, Generic, TypeVar +from typing import Any, ClassVar, Generic, TypeVar, overload from typing_extensions import ParamSpec, final if sys.version_info >= (3, 9): @@ -11,7 +11,10 @@ _P = ParamSpec("_P") @final class ContextVar(Generic[_T]): - def __init__(self, name: str, *, default: _T = ...) -> None: ... + @overload + def __init__(self, name: str) -> None: ... + @overload + def __init__(self, name: str, *, default: _T) -> None: ... @property def name(self) -> str: ... def get(self, default: _T = ...) -> _T: ...