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

Correct type annotations on two fields in _Arguments that are optional but are not marked as such #16897

Merged
merged 3 commits into from
Feb 11, 2024

Conversation

sam-xif
Copy link
Contributor

@sam-xif sam-xif commented Feb 9, 2024

Hello mypy team,

I am working as part of a research team developing a code analysis tool for Python. One of the issues the tool discovered in mypy's codebase is that two fields in the _Arguments class, mypy_config_file and custom_typeshed_dir, can take on a None value, but they are not marked as such. Calling stubtest.parse_options on an empty list of arguments reproduces the situation where these two fields are None.

If you are interested in learning more about the tool and how it found this issue, let me know down in the comments, or you can contact me at [email protected]. If you find that this issue is not legitimate, I would be interested in understanding why.

Note that I did not add any test cases as no functional changes to the code were made.

Thank you for your consideration!

-Sam

Copy link
Collaborator

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

The change looks correct. The code already handles the None case, so it's not an issue in reality. Nevertheless good to change it.

Just to satisfy the typing, could you adjust the abspath call a few lines below?

mypy/mypy/stubtest.py

Lines 1923 to 1925 in 517f5ae

options.custom_typeshed_dir = args.custom_typeshed_dir
if options.custom_typeshed_dir:
options.abs_custom_typeshed_dir = os.path.abspath(args.custom_typeshed_dir)

     if options.custom_typeshed_dir:
-        options.abs_custom_typeshed_dir = os.path.abspath(args.custom_typeshed_dir)
+        options.abs_custom_typeshed_dir = os.path.abspath(options.custom_typeshed_dir)

@sam-xif
Copy link
Contributor Author

sam-xif commented Feb 9, 2024

@cdce8p just saw your comment right after I pushed a change to resolve the type error. Instead of making the change you suggested, I changed the if condition to check if args.custom_typeshed_dir is truthy. I believe they are equivalent, but do you have a preference?

@cdce8p
Copy link
Collaborator

cdce8p commented Feb 9, 2024

I would suggest to use the value from options. That better matches what we do for other values too. Directly below for mypy_config_file. We assign it to options.config_file and just use that afterwards.

mypy/mypy/stubtest.py

Lines 1926 to 1929 in 517f5ae

options.config_file = args.mypy_config_file
options.use_builtins_fixtures = use_builtins_fixtures
if options.config_file:

@sam-xif
Copy link
Contributor Author

sam-xif commented Feb 9, 2024

@cdce8p Makes sense, thanks. Made the change you suggested.

Copy link
Collaborator

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @sam-xif 👍🏻

@hauntsaninja
Copy link
Collaborator

Thanks!

@hauntsaninja hauntsaninja merged commit 837f7e0 into python:master Feb 11, 2024
12 of 13 checks passed
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.

4 participants