Skip to content
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

Improve annotations for various set methods #11403

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AlexWaygood
Copy link
Member

It's a bit unfortunate how many type: ignores we have to introduce here, but I can't see a good way round them. I've added some test cases to make sure everything's working correctly.

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Feb 11, 2024

Primer analysis:

@AlexWaygood AlexWaygood marked this pull request as ready for review February 11, 2024 16:59

This comment has been minimized.

@srittau
Copy link
Collaborator

srittau commented Mar 23, 2024

This has conflicts now.

@AlexWaygood
Copy link
Member Author

This has conflicts now.

I fixed the merge conflicts, but it's still not clear to me whether we want to fix the inconsistency between the different methods by making some annotations stricter (as this PR does), which seems to lead to false positives when running type checkers on user code. I think a better solution might be to make the methods consistent by making some annotations looser instead -- what do you think?

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/v1/main.py:901: error: Set comprehension has incompatible type Set[int | str]; expected Set[None]  [misc]
+ pydantic/main.py:1268: error: Argument 1 to "set" has incompatible type "AbstractSet[int] | AbstractSet[str] | Mapping[int, Any] | Mapping[str, Any]"; expected "Iterable[str | None]"  [arg-type]
+ pydantic/deprecated/copy_internals.py:222: error: Set comprehension has incompatible type Set[int | str]; expected Set[str | None]  [misc]

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/datastructures/structures.pyi:197: error: Argument 1 of "discard" is incompatible with supertype "set"; supertype defines the argument type as "Optional[str]"  [override]
+ src/werkzeug/datastructures/structures.pyi:197: note: This violates the Liskov substitution principle
+ src/werkzeug/datastructures/structures.pyi:197: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
+ src/werkzeug/datastructures/structures.pyi:197: error: Argument 1 of "discard" is incompatible with supertype "MutableSet"; supertype defines the argument type as "Optional[str]"  [override]

jax (https://github.com/google/jax)
+ jax/_src/lax/parallel.py:550: error: Need type annotation for "subs_batch" (hint: "subs_batch: Set[<type>] = ...")  [var-annotated]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants