Skip to content

Commit 9c4c192

Browse files
committed
Avoid class-scope name collisions in stubs
Class members such as list, type, cursor, Model, and datetime shadow the builtins, classes, or modules referenced by nearby annotations. ty then resolves those annotations to Unknown, which can hide invalid calls in APIs including docker, sqlite3, psycopg2, Markdown, and requests. Qualify shadowed builtins and use private aliases for colliding imports and classes. This removes 15 stdlib and 120 third-party collision diagnostics under ty 0.0.58, restores the affected public types, and fixes four existing psycopg2 type assertions.
1 parent f76037a commit 9c4c192

29 files changed

Lines changed: 176 additions & 140 deletions

File tree

stdlib/_curses_panel.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _curses import window
1+
from _curses import window as _window
22
from typing import Final, final
33

44
__version__: Final[str]
@@ -14,14 +14,14 @@ class panel:
1414
def hidden(self) -> bool: ...
1515
def hide(self) -> None: ...
1616
def move(self, y: int, x: int, /) -> None: ...
17-
def replace(self, win: window, /) -> None: ...
17+
def replace(self, win: _window, /) -> None: ...
1818
def set_userptr(self, obj: object, /) -> None: ...
1919
def show(self) -> None: ...
2020
def top(self) -> None: ...
2121
def userptr(self) -> object: ...
22-
def window(self) -> window: ...
22+
def window(self) -> _window: ...
2323

2424
def bottom_panel() -> panel: ...
25-
def new_panel(win: window, /) -> panel: ...
25+
def new_panel(win: _window, /) -> panel: ...
2626
def top_panel() -> panel: ...
2727
def update_panels() -> panel: ...

stdlib/multiprocessing/managers.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
import queue
23
import sys
34
import threading
@@ -359,9 +360,9 @@ class SyncManager(BaseManager):
359360
@overload
360361
def dict(self, iterable: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> DictProxy[str, _VT]: ...
361362
@overload
362-
def dict(self, iterable: Iterable[list[str]], /) -> DictProxy[str, str]: ...
363+
def dict(self, iterable: Iterable[builtins.list[str]], /) -> DictProxy[str, str]: ...
363364
@overload
364-
def dict(self, iterable: Iterable[list[bytes]], /) -> DictProxy[bytes, bytes]: ...
365+
def dict(self, iterable: Iterable[builtins.list[bytes]], /) -> DictProxy[bytes, bytes]: ...
365366

366367
# Overloads are copied from builtins.list.__init__
367368
@overload

stdlib/multiprocessing/pool.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Iterable, Mapping
2-
from multiprocessing.context import DefaultContext, Process
2+
from multiprocessing.context import DefaultContext, Process as _Process
33
from types import GenericAlias, TracebackType
44
from typing import Any, Final, Generic, TypeVar
55
from typing_extensions import Self
@@ -50,7 +50,7 @@ class Pool:
5050
context: Any | None = None,
5151
) -> None: ...
5252
@staticmethod
53-
def Process(ctx: DefaultContext, *args: Any, **kwds: Any) -> Process: ...
53+
def Process(ctx: DefaultContext, *args: Any, **kwds: Any) -> _Process: ...
5454
def apply(self, func: Callable[..., _T], args: Iterable[Any] = (), kwds: Mapping[str, Any] = {}) -> _T: ...
5555
def apply_async(
5656
self,

stdlib/sqlite3/__init__.pyi

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,28 +269,39 @@ class OperationalError(DatabaseError): ...
269269
class ProgrammingError(DatabaseError): ...
270270
class Warning(Exception): ...
271271

272+
_DataError: TypeAlias = DataError
273+
_DatabaseError: TypeAlias = DatabaseError
274+
_Error: TypeAlias = Error
275+
_IntegrityError: TypeAlias = IntegrityError
276+
_InterfaceError: TypeAlias = InterfaceError
277+
_InternalError: TypeAlias = InternalError
278+
_NotSupportedError: TypeAlias = NotSupportedError
279+
_OperationalError: TypeAlias = OperationalError
280+
_ProgrammingError: TypeAlias = ProgrammingError
281+
_Warning: TypeAlias = Warning
282+
272283
@disjoint_base
273284
class Connection:
274285
@property
275-
def DataError(self) -> type[DataError]: ...
286+
def DataError(self) -> type[_DataError]: ...
276287
@property
277-
def DatabaseError(self) -> type[DatabaseError]: ...
288+
def DatabaseError(self) -> type[_DatabaseError]: ...
278289
@property
279-
def Error(self) -> type[Error]: ...
290+
def Error(self) -> type[_Error]: ...
280291
@property
281-
def IntegrityError(self) -> type[IntegrityError]: ...
292+
def IntegrityError(self) -> type[_IntegrityError]: ...
282293
@property
283-
def InterfaceError(self) -> type[InterfaceError]: ...
294+
def InterfaceError(self) -> type[_InterfaceError]: ...
284295
@property
285-
def InternalError(self) -> type[InternalError]: ...
296+
def InternalError(self) -> type[_InternalError]: ...
286297
@property
287-
def NotSupportedError(self) -> type[NotSupportedError]: ...
298+
def NotSupportedError(self) -> type[_NotSupportedError]: ...
288299
@property
289-
def OperationalError(self) -> type[OperationalError]: ...
300+
def OperationalError(self) -> type[_OperationalError]: ...
290301
@property
291-
def ProgrammingError(self) -> type[ProgrammingError]: ...
302+
def ProgrammingError(self) -> type[_ProgrammingError]: ...
292303
@property
293-
def Warning(self) -> type[Warning]: ...
304+
def Warning(self) -> type[_Warning]: ...
294305
@property
295306
def in_transaction(self) -> bool: ...
296307
isolation_level: _IsolationLevel

stubs/Markdown/markdown/blockparser.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from collections.abc import Iterable
22
from typing import Any, TypeVar
33
from xml.etree.ElementTree import Element, ElementTree
44

5-
from markdown import blockprocessors, util
5+
from markdown import blockprocessors as _blockprocessors, util
66
from markdown.core import Markdown
77

88
_T = TypeVar("_T")
@@ -13,7 +13,7 @@ class State(list[_T]):
1313
def isstate(self, state: _T) -> bool: ...
1414

1515
class BlockParser:
16-
blockprocessors: util.Registry[blockprocessors.BlockProcessor]
16+
blockprocessors: util.Registry[_blockprocessors.BlockProcessor]
1717
state: State[Any] # TODO: possible to get rid of Any?
1818
md: Markdown
1919
def __init__(self, md: Markdown) -> None: ...

stubs/Markdown/markdown/core.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ from typing import Any, ClassVar, Literal
55
from typing_extensions import Self
66
from xml.etree.ElementTree import Element
77

8-
from . import blockparser, inlinepatterns, postprocessors, preprocessors, treeprocessors
8+
from . import (
9+
blockparser,
10+
inlinepatterns,
11+
postprocessors as _postprocessors,
12+
preprocessors as _preprocessors,
13+
treeprocessors as _treeprocessors,
14+
)
915
from .extensions import Extension
1016
from .util import HtmlStash, Registry
1117

@@ -14,10 +20,10 @@ __all__ = ["Markdown", "markdown", "markdownFromFile"]
1420
logger: Logger
1521

1622
class Markdown:
17-
preprocessors: Registry[preprocessors.Preprocessor]
23+
preprocessors: Registry[_preprocessors.Preprocessor]
1824
inlinePatterns: Registry[inlinepatterns.Pattern]
19-
treeprocessors: Registry[treeprocessors.Treeprocessor]
20-
postprocessors: Registry[postprocessors.Postprocessor]
25+
treeprocessors: Registry[_treeprocessors.Treeprocessor]
26+
postprocessors: Registry[_postprocessors.Postprocessor]
2127
parser: blockparser.BlockParser
2228
htmlStash: HtmlStash
2329
output_formats: ClassVar[dict[Literal["xhtml", "html"], Callable[[Element], str]]]

stubs/WebOb/webob/cachecontrol.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
from _typeshed import SupportsItems
23
from collections.abc import Callable
34
from typing import Any, Generic, Literal, overload
@@ -68,7 +69,7 @@ class value_property(Generic[_T, _DefaultT, _NoneLiteral, _ScopeT]):
6869

6970
class CacheControl(Generic[_ScopeT]):
7071
header_value: str
71-
update_dict: type[UpdateDict]
72+
update_dict: builtins.type[UpdateDict]
7273
properties: dict[str, Any]
7374
type: _ScopeT
7475
def __init__(self, properties: dict[str, Any], type: _ScopeT) -> None: ...

stubs/colorful/colorful/core.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Colorful:
6666
def __init__(self, colormode: _ColorModeType | None = None, colorpalette: _str | _PaletteType | None = None) -> None: ...
6767

6868
@property
69-
def colorpalette(self) -> SupportsItems[str, str | tuple[int, int, int]] | None: ...
69+
def colorpalette(self) -> SupportsItems[_str, _str | tuple[int, int, int]] | None: ...
7070
@colorpalette.setter
7171
def colorpalette(self, colorpalette: _str | _PaletteType) -> None: ...
7272

stubs/docker/docker/models/configs.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import builtins
2+
13
from .resource import Collection, Model
24

35
class Config(Model):
@@ -10,4 +12,4 @@ class ConfigCollection(Collection[Config]):
1012
model: type[Config]
1113
def create(self, **kwargs) -> Config: ... # type: ignore[override]
1214
def get(self, config_id: str) -> Config: ...
13-
def list(self, **kwargs) -> list[Config]: ...
15+
def list(self, **kwargs) -> builtins.list[Config]: ...

0 commit comments

Comments
 (0)