GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — allow arbitrary types in set difference by randolf-scholz · Pull Request #15160 · python/typeshed

Conversation

@randolf-scholz

@randolf-scholz randolf-scholz commented Dec 21, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fixes #9004
Partially Fixes #15271
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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions

This comment has been minimized.

Sign in to view
@randolf-scholz
allow arbitrary types in set difference
57c542c
@randolf-scholz
randolf-scholz force-pushed the set_sub branch from db50008 to 57c542c Compare January 10, 2026 09:52
@randolf-scholz
updated test
f7af3f7
@github-actions

This comment has been minimized.

Sign in to view
This was referenced Jan 10, 2026
Improved Literal? types python/mypy#20444
Open
Improve annotations for various set methods #11403
Open
@randolf-scholz
Merge branch 'main' into set_sub
8531886
@github-actions

github-actions Bot commented Jan 16, 2026

Copy link
Copy Markdown
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:869: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/core/dtypes/cast.py:870: 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:924: error: Set comprehension has incompatible type Set[int | str]; expected Set[str | None]  [misc]
- pydantic/v1/main.py:924: note: Left operand is of type "set[str] | dict_keys[str, Any]"
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

srittau
srittau approved these changes Jan 16, 2026
View reviewed changes

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm generally not a big fan of allowing object in cases like this, but the primer output (and consistency with __isub__) speaks for itself. Also, {1,2,3} - {1,2,"ab"} is a valid use case.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@randolf-scholz

randolf-scholz commented Jan 16, 2026

Copy link
Copy Markdown
Contributor Author

@srittau Maybe we should be using Any rather than object? This is what AbstractSet does. Might be worth it if at any point in the future, instead of object we would like to use Hashable instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz

randolf-scholz commented Jan 16, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I'd also like to get @AlexWaygood's opinion, as you discussed changing set.__sub__ a bit in #11403

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jan 16, 2026

Copy link
Copy Markdown
Member

This seems like a good change to me. I agree that when typeshed is too opinionated, it often causes more problems than it solves; we've been running into a few pain points on the ty issue tracker recently with similar causes.

Please let's not (ever) change the parameter type to Hashable, though. Attempting to represent hashability in Python's type system is doomed by the fact that object.__hash__ exists, so the many types that are not hashable all violate the Liskov Substitution Principle and thus a large number of assumptions that type checkers make. This means that you get unsoundness and inconsistent behaviour from all type checkers if you annotate types with Hashable (though ty is affected more than most currently). (See astral-sh/ty#1132 (comment), astral-sh/ty#1162, astral-sh/ty#1846, PyCQA/flake8-pyi#283 (comment), etc.)

randolf-scholz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz

randolf-scholz commented Jan 16, 2026

Copy link
Copy Markdown
Contributor Author

Yeah, that whole hashability thing is a big mess. I also recently learned one can make unhashable classes via custom metaclasses, which is concerning.

Anyway, so is this good, or are there any other reasons to prefer Any vs object here? I chose object simply because that is what was already there for __isub__...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jan 16, 2026

Copy link
Copy Markdown
Member

I think either is fine; it doesn't really make much difference for parameter annotations. We sometimes use Any in parameter annotations to represent "the 'true type' is inexpressible", and I guess that applies here because of the hashability mess, but I think object is fine

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood approved these changes Jan 16, 2026
View reviewed changes
@AlexWaygood
AlexWaygood merged commit 236a8c0 into python:main Jan 16, 2026
@AlexWaygood

AlexWaygood commented Jan 16, 2026

Copy link
Copy Markdown
Member

Thanks @randolf-scholz!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz randolf-scholz mentioned this pull request Jan 16, 2026
Several methods on built-in types are too strict. #15271
Open
1 of 9 tasks
@cdce8p cdce8p mentioned this pull request Jan 17, 2026
Sync typeshed python/mypy#20605
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@srittau srittau srittau approved these changes
@AlexWaygood AlexWaygood AlexWaygood approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Several methods on built-in types are too strict. Annotations for set.__sub__ and related methods are inconsistent

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@randolf-scholz @AlexWaygood @srittau
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.