Skip to content

Conversation

@randolf-scholz
Copy link
Contributor

Fixes #9004
See also: #11403

This PR allows set differences with disjoint generic types. This option was previously argued against (#9004 (comment)) because

A user shipped code that did set[bytes] - set[str] to production, and of course, it didn't work as expected (#1840). There is no use case for set[int] - set[str]: if your code does it, that's very likely not intentional.

However, typeshed shouldn't try to act as a linter. That's the job of the type checker. There are cases when the current setup hurts, for instance when we try to do set[Literal["a", "b"]] - set[str].

This should be handled on the type-checker side, for instance by warning on set[T] - set[S] if T and S have empty intersection (⇝ disjoint_bases https://peps.python.org/pep-0800/), which would catch the bad case described above.

@github-actions
Copy link
Contributor

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

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/dtypes/cast.py:870: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/core/dtypes/cast.py:871: error: Unused "type: ignore" comment  [unused-ignore]

ibis (https://github.com/ibis-project/ibis)
- ibis/backends/sql/datatypes.py:1398: error: Unsupported operand types for - ("set[type[Never]]" and "set[type[SqlglotType]]")  [operator]
- ibis/expr/datatypes/tests/test_core.py:581: error: Unsupported operand types for - ("set[type[Never]]" and "set[AnnotableMeta]")  [operator]

schemathesis (https://github.com/schemathesis/schemathesis)
- src/schemathesis/config/_error.py:54: error: Argument 1 to "set" has incompatible type "Any | Unset"; expected "Iterable[Any | None]"  [arg-type]
+ src/schemathesis/config/_error.py:54: error: Argument 1 to "set" has incompatible type "Any | Unset"; expected "Iterable[object]"  [arg-type]

pydantic (https://github.com/pydantic/pydantic)
- pydantic/v1/main.py:907: error: Set comprehension has incompatible type Set[int | str]; expected Set[str | None]  [misc]
- pydantic/v1/main.py:907: note: Left operand is of type "set[str] | dict_keys[str, Any]"

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.

Annotations for set.__sub__ and related methods are inconsistent

1 participant