Skip to content

fix list.copy to use the expected type#14755

Open
KotlinIsland wants to merge 3 commits into
python:mainfrom
KotlinIsland:list.copy
Open

fix list.copy to use the expected type#14755
KotlinIsland wants to merge 3 commits into
python:mainfrom
KotlinIsland:list.copy

Conversation

@KotlinIsland

@KotlinIsland KotlinIsland commented Sep 21, 2025

Copy link
Copy Markdown
Contributor

support:

_: list[object] = list[int]().copy()

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread stdlib/builtins.pyi
@overload
def __init__(self, iterable: Iterable[_T], /) -> None: ...
def copy(self) -> list[_T]: ...
def copy(self) -> list[_S | _T]: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

_S has no meaning, as it's only used once in this context. It's just interpreted as Any in this context.

@srittau srittau Sep 21, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see what you're trying to do, but we should probably use:

Suggested change
def copy(self) -> list[_S | _T]: ...
# The new list can allow broader types that the old one. `Any` allows that.
def copy(self) -> list[_T | Any]: ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

_S has no meaning, as it's only used once in this context. It's just interpreted as Any in this context.

that is demonstrably not true

This comment was marked as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

basedpyright is incorrect there. A[int | Any] should be assignable to A[object].

@KotlinIsland KotlinIsland Sep 21, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

consider this example:

def empty_list[T=Never]() -> list[T]:
    return []

a = empty_list()
b: list[int] = empty_list()

if we were to use Any here, then the type of a would be list[Any]

additionally, there would be absolutely no safety within empty_list to return the correct type. i'm not really sure where this idea that type variables only in return positions being invalid came from

this is literally the meaning of a type variable, to use Any is a mistake

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

basedpyright is incorrect there. A[int | Any] should be assignable to A[object].

yes: microsoft/pyright#10713 (comment)

i was a little hasty with the first example, i think it's clear though that this case is the intended use of a type variable, not Any

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah I think this does make sense, let's do it.

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants