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

Add typing for context managers and methods returning Self in non-generated python code #2674

Merged
merged 3 commits into from
Jan 22, 2025

Conversation

echoix
Copy link
Contributor

@echoix echoix commented Jan 18, 2025

This is a smaller, conservative PR that doesn't step on the toes of #2668.
It adds a return type annotation for typing.Self, introduced in Python 3.11, or coming from typing_extensions otherwise. https://typing-extensions.readthedocs.io/en/latest/#typing_extensions.Self
https://docs.python.org/3/library/typing.html#typing.Self

It is for non-generated files.

I did an attempt to generate it for all the context managers via the etg scripts, but while it worked, it imported multiple times the Self throughout the files (the runtime ones, not only pyi), as I only found how to use c.addPyCode(), which isn't exactly appropriate to define an import that should be deduplicated and placed at top. Maybe SIP has a more specialized directive for that that would allow deduplicating these imports when combining different files together. So I simply did not include it yet.

Something like this, where the imports are not deduplicated, and can appear multiple times:

    # context manager methods
    c.addPyCode("""\
import sys
if sys.version_info >= (3, 11):
    from typing import Self
else:
    from typing_extensions import Self
    """)
    c.addPyMethod('__enter__', '(self) -> Self', 'return self')

@swt2c swt2c force-pushed the typing-self-python-only branch from da542b9 to 5ee3aa1 Compare January 22, 2025 02:39
@swt2c
Copy link
Collaborator

swt2c commented Jan 22, 2025

Thanks a lot, rebased due to merge conflict with your other PR.

@swt2c swt2c merged commit 744801f into wxWidgets:master Jan 22, 2025
2 checks passed
@lojack5
Copy link
Contributor

lojack5 commented Jan 22, 2025

FYI: <3.10 still needs typing_extensions due to the use of typing.ParamSpec.

@echoix
Copy link
Contributor Author

echoix commented Jan 22, 2025

This PR has it bumped higher since Self was introduced in 3.11, so we're good

@lojack5
Copy link
Contributor

lojack5 commented Jan 22, 2025

Oh right, my math brain was backwards.

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.

3 participants