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

Use a more precise return type for tokenize.untokenize() #13366

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

Conversation

tomasr8
Copy link
Member

@tomasr8 tomasr8 commented Jan 5, 2025

No description provided.

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Hi, @tomasr8! We usually avoid having union return types. See https://github.com/python/typeshed/blob/main/CONTRIBUTING.md#conventions

@Akuli
Copy link
Collaborator

Akuli commented Jan 13, 2025

We might be able to use the Any trick. If we use str | Any as the return type, it means that you can either assume it's a string (most common use case), or use assert isinstance(result, bytes) or similar if you expect a bytes result.

@tomasr8
Copy link
Member Author

tomasr8 commented Jan 13, 2025

We might be able to use the Any trick. If we use str | Any as the return type, it means that you can either assume it's a string (most common use case), or use assert isinstance(result, bytes) or similar if you expect a bytes result.

This seems like the better option to me, as opposed to leaving it completely untyped

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

Let's try the Any trick.

stdlib/tokenize.pyi Outdated Show resolved Hide resolved
stdlib/tokenize.pyi Outdated Show resolved Hide resolved
Copy link
Contributor

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

schemathesis (https://github.com/schemathesis/schemathesis)
-   File "/tmp/mypy_primer/old_mypy/venv/bin/mypy", line 8, in <module>
+   File "/tmp/mypy_primer/new_mypy/venv/bin/mypy", line 8, in <module>
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
+   File "/tmp/mypy_primer/new_mypy/venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry

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