Skip to content

Add @disjoint_base decorator in the stdlib #14599

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

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

Conversation

JelleZijlstra
Copy link
Member

No description provided.

This comment has been minimized.

@AlexWaygood
Copy link
Member

I almost wonder if it's worth adding a variant is_disjoint_base_if, which you can pass a condition to providing the type checker can statically infer its truthiness. E.g.

@disjoint_base_if(sys.version_info < (3, 11))
class Foo(tuple): ...

Maybe that would just be overcomplicating the proposal, though

@JelleZijlstra
Copy link
Member Author

I've been thinking about a more general version, where we add a do-nothing identity decorator so you can write @(disjoint_base if sys.version_info >= (3, 12) else identity). Maybe not worth it though.

Pyright seems very unhappy here presumably because I added one too many sys.version_info branches, will try to figure out which one pushed it over the edge.

Some of the remaining stubtest errors are false positives, fixing in python/mypy#19701

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I think for pure-Python classes that are disjoint bases because they have __slots__, we should generally just add the __slots__ rather than adding the @disjoint_base decorator? That will help type checkers in other ways too (#8832). I highlighted a few here, but I think there are probably some more

@JelleZijlstra
Copy link
Member Author

I think for pure-Python classes that are disjoint bases because they have slots, we should generally just add the slots rather than adding the @disjoint_base decorator? That will help type checkers in other ways too (#8832). I highlighted a few here, but I think there are probably some more

Yes, I agree, but I'd rather not have to look at all of these classes manually.

@AlexWaygood
Copy link
Member

I've been thinking about a more general version, where we add a do-nothing identity decorator so you can write @(disjoint_base if sys.version_info >= (3, 12) else identity). Maybe not worth it though.

yes, that's quite appealing, actually, from my perspective as a typeshed maintainer -- it would also be useful for deprecations, since we have an increasing number of sys.version_info branches where the only difference between the branches is that a function is marked with @deprecated in one branch but not in the other.

Although that's actually quite convenient for us over at ty, because the deprecation notice is often also reflected in the docstring, and docstring-adder will add the version-specific docstrings to the two branches.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM if you fix the TarInfo CI failure!

@JelleZijlstra
Copy link
Member Author

I think I need #14626 for CI to go green. I'll also wait for my mypy PR to be merged so I can pin stubtest to a commit from mypy master rather than a random one from my fork.

Copy link
Contributor

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

ibis (https://github.com/ibis-project/ibis)
- ...typeshed_to_test/stdlib/builtins.pyi:115: note: "Any" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:117: note: "Any" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:115: note: "__init__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:117: note: "__init__" of "object" defined here

strawberry (https://github.com/strawberry-graphql/strawberry)
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here

prefect (https://github.com/PrefectHQ/prefect)
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:137: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:139: note: "__init_subclass__" of "object" defined here

zulip (https://github.com/zulip/zulip)
- ...typeshed_to_test/stdlib/builtins.pyi:115: note: "SubTest" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:117: note: "SubTest" defined here

discord.py (https://github.com/Rapptz/discord.py)
- ...typeshed_to_test/stdlib/typing.pyi:1042: note: "update" of "TypedDict" defined here
+ ...typeshed_to_test/stdlib/typing.pyi:1043: note: "update" of "TypedDict" defined here
- discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
+ discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
- discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
+ discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
- discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]

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.

2 participants