Skip to content

Fix TypeVars in beautifulsoup and SQLAlchemy #8087

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 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions stubs/SQLAlchemy/sqlalchemy/orm/decl_api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,29 @@ class registry:
def mapped(self, cls: _ClsT) -> _ClsT: ...
# Return type of the callable is a _DeclarativeBase class with the passed in class as base.
# This could be better approximated with Intersection[PassedInClass, _DeclarativeBase].
@overload
def as_declarative_base(self, *, mapper: Any | None = ...) -> Callable[[_ClsT], _ClsT | DeclarativeMeta | Any]: ...
@overload
def as_declarative_base(
self, *, mapper: Any | None = ..., metaclass: _DeclarativeBaseMeta[_DeclT] = ...
self, *, mapper: Any | None = ..., metaclass: _DeclarativeBaseMeta[_DeclT]
) -> Callable[[_ClsT], _ClsT | _DeclT | Any]: ...
def map_declaratively(self, cls): ...
def map_imperatively(self, class_, local_table: Any | None = ..., **kw): ...

@overload
def as_declarative(
*,
bind: Connectable | None = ...,
metadata: MetaData | None = ...,
class_registry: dict[str, type[Any]] | None = ...,
mapper: Any | None = ...,
metaclass: _DeclarativeBaseMeta[_DeclT] = ...,
) -> Callable[[_ClsT], _ClsT | DeclarativeMeta | Any]: ...
@overload
def as_declarative(
*,
bind: Connectable | None = ...,
metadata: MetaData | None = ...,
class_registry: dict[str, type[Any]] | None = ...,
mapper: Any | None = ...,
metaclass: _DeclarativeBaseMeta[_DeclT],
) -> Callable[[_ClsT], _ClsT | _DeclT | Any]: ...
5 changes: 4 additions & 1 deletion stubs/beautifulsoup4/bs4/element.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,7 @@ class SoupStrainer:

class ResultSet(list[_PageElementT], Generic[_PageElementT]):
source: SoupStrainer
def __init__(self, source: SoupStrainer, result: Iterable[_PageElementT] = ...) -> None: ...
@overload
def __init__(self, source: SoupStrainer) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I haven't following this so far, but shouldn't this annotate self to bind _PageElementT?

Copy link
Member Author

@AlexWaygood AlexWaygood Jun 16, 2022

Choose a reason for hiding this comment

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

This is basically the same approach that we've had for a while for some generic collections in builtins and collections, e.g. this has been the first overload for builtins.list for (at least) two years now:

class list(MutableSequence[_T], Generic[_T]):
@overload
def __init__(self) -> None: ...

If the constructor-method overload picked is one where there are no TypeVars present at all, despite the class being generic, the type checker will require the user to provide an explicit type annotation, the same as happens if you try to initialise an empty list 🙂

https://mypy-play.net/?mypy=latest&python=3.10&gist=dcbf1006ff7606d02d5670171bc6cae1

@overload
def __init__(self, source: SoupStrainer, result: Iterable[_PageElementT]) -> None: ...