Skip to content

Collection is Sized #8977

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 7 commits into from
Oct 25, 2022
Merged

Conversation

sobolevn
Copy link
Member

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Hm, the decision not to implement Sized in Collection seems pretty intentional.

Looks like this goes back to #2658 , and was maybe done for Python 2 reasons?

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Considering that primer does not complain and the change back in 2018 alludes to Python 2/3 problems or problems that may be fixed in mypy 0.650, this looks good to me. But maybe @gvanrossum has some recollection about this?

@sobolevn
Copy link
Member Author

Or @JukkaL :)

@JukkaL
Copy link
Contributor

JukkaL commented Oct 25, 2022

This change seems fine, since the original motivation was Python 2/3 compatibility concerns, and the mypy primer output is clean.

@sobolevn
Copy link
Member Author

I think that it is a good idea to copy original samples to our test cases.
Please, wait a moment :)

@sobolevn
Copy link
Member Author

sobolevn commented Oct 25, 2022

pyright has a different opinion:

/home/runner/work/typeshed/typeshed/test_cases/stdlib/typing/check_aliases.py:4:13 - error: "assert_type" mismatch: expected "list[Collection[str]]" but received "list[str | set[str]]"

Ok, a test case won't quite work.

But, here's my manual test case for this:

(.venv) ~/Desktop/typeshed  collection-is-sized ✔                                    
» mypy -c "reveal_type(['a', {'b'}])" --custom-typeshed-dir .
<string>:1: note: Revealed type is "builtins.list[typing.Collection[builtins.str]]"
Success: no issues found in 1 source file

(.venv) ~/Desktop/typeshed  collection-is-sized ✔                                    
» mypy -c "reveal_type(['a', ['b']])" --custom-typeshed-dir .
<string>:1: note: Revealed type is "builtins.list[typing.Sequence[builtins.str]]"
Success: no issues found in 1 source file

@sobolevn sobolevn closed this Oct 25, 2022
@sobolevn sobolevn reopened this Oct 25, 2022
@github-actions
Copy link
Contributor

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

@JelleZijlstra JelleZijlstra merged commit 5bbba5d into python:master Oct 25, 2022
@hauntsaninja
Copy link
Collaborator

Looks like this change breaks mypy: python/mypy#13977

Reproduce with e.g. mypy -c 'reveal_type(5)' --no-silence-site-packages --custom-typeshed-dir ~/dev/typeshed

The two quickest fixes are either:

diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi
index d03a3eab..c9cfe304 100644
--- a/stdlib/typing.pyi
+++ b/stdlib/typing.pyi
@@ -325,7 +325,7 @@ class SupportsRound(Protocol[_T_co]):
     def __round__(self, __ndigits: int) -> _T_co: ...
 
 @runtime_checkable
-class Sized(Protocol, metaclass=ABCMeta):
+class Sized(Protocol):
     @abstractmethod
     def __len__(self) -> int: ...

or:

diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi
index d03a3eab..bfb7cfa7 100644
--- a/stdlib/typing.pyi
+++ b/stdlib/typing.pyi
@@ -452,7 +452,9 @@ class Container(Protocol[_T_co]):
     def __contains__(self, __x: object) -> bool: ...
 
 @runtime_checkable
-class Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
+class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
+    @abstractmethod
+    def __len__(self) -> int: ...
 
 class Sequence(Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
     @overload

Which do people prefer?

@JelleZijlstra
Copy link
Member

I'd prefer removing the metaclass, I don't think we need it there.

@jab
Copy link
Contributor

jab commented Nov 29, 2022

I think this change caused a false positive error that I'm seeing. More info in #9296.

jab added a commit to jab/typeshed that referenced this pull request Dec 10, 2022
JelleZijlstra pushed a commit that referenced this pull request Dec 10, 2022
* Revert "`Collection` is `Sized` (#8977)"

This reverts commit 5bbba5d.

* Revert "typing: remove metaclass from Sized (#9058)"

This reverts commit a3ce512.

* Add regression test for issue 9296.
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.

6 participants