-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
fix: closes #11343's [attr-defined] type errors #11345
Merged
nicoddemus
merged 9 commits into
pytest-dev:main
from
WarrenTheRabbit:add-type-ignore-for-attr-defined-errors
Aug 27, 2023
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a91c63
fix: closes #11343's [attr-defined] type errors
WarrenTheRabbit 7600243
Merge branch 'main' into add-type-ignore-for-attr-defined-errors
WarrenTheRabbit 15ebcf5
refactor: replace type:ignore with platform check
WarrenTheRabbit fe67feb
Merge branch 'add-type-ignore-for-attr-defined-errors' of https://git…
WarrenTheRabbit 2d5bfa8
refactor: add self-documenting UNRELIABLE keyword
WarrenTheRabbit f9aa628
refactor: self-document error flag with a constant
WarrenTheRabbit af4ebeb
doc: improve explanation of return values
WarrenTheRabbit 60f6566
refactor: change name of self-documenting constant
WarrenTheRabbit c7b602c
doc: remove unwanted implication
WarrenTheRabbit File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
Would this pass if the else block was used?
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.
Hi, Ronny. Thanks for taking a look. I'll need a clarification, and I also thank you for your patience while I'm learning.
Are you asking
1. what happens if I remove the
type: ignore
and use anelse
block instead?2. what happens when the current code is executed through the implied else block (lines 322 to 325)?
3. something else?
Responding to 1 (use an
else
block instead)The Python docs state that
os.getuid
only has availability on Unix. Since my system is Windows, changing the code tostill fails the mypy hook:
But either option (using
type: ignore
or an explicitif: else:
block) passes when I run the hook on VS Code's WSL.I'm still very new and I'm trying to understand the motivation behind the question. Are there advantages to having an
else
block instead of thetype: ignore
comment - such that you would prefer it, if possible?Responding to 2 (execute through lines 321 - 325)
The function seems to work fine when executed on a Windows system (it halts at the guard on line 320 and returns
None
) and a Unix system (it completes execution at 325 and returnsuid if uid != -1 else None
.If this is the heart of your question, would you like me to write a test that demonstrates that?
Responding to 3
I worry I have not understood the underlying concern and that my responses do not address them!
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.
Thanks for the response,
I was wondering about 1
My concerns are resolved
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.
Is someone able to give feedback on my communication? I've reflected and think this would have been easier for Ronny:
I know everyone is busy. But I'd like to flag that I am always open to and welcome feedback. I'm still learning. Moreover, I want to be a very harmonious and easy-to-work-with contributor.
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.
You're fine in my book 🙃 I didn't understand what Ronny meant exactly either. If he would have meant something else, your detailed answer would have prevented another round-trip.
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.
@WarrenTheRabbit it's always fine to ask for clarifications. Let me explain a bit more what (I think) @RonnyPfannschmidt was aiming for.
Some functions, like
os.getuid
, are only defined on certain platforms. This is reflected in the type definition ofos.getuid
in typeshed, the project which defines the typing for all of the standard library. See here:https://github.com/python/typeshed/blob/a094aa09c2aa47721664d3fdef91eda4fac24ebb/stdlib/os/__init__.pyi#L460-L476
You can see that
os.getuid
is only defined whensys.platform != "win32"
.Now, mypy is a static type checker, and such platform-dependence puts it in a conundrum - which platform should it use when type checking? This can cause meaningful differences, like in this example - when run on
linux
platform (like CI) there is no error, but on Windows there is. What mypy does is to type-check using the platform it is running on, but you can ask it use a different platform using for examplemypy --platform win32
ormypy --platform linux
.So we see that mypy can give different results on different platforms, that's not great. But it turns out that mypy does provide a solution to this -- it understands
if sys.platform == "..."
checks and knows to ignore either theif
or theelse
according to the platform it's using. See here for the mypy docs on this.OK, if mypy does this then why doesn't work here? The reason is that while mypy is smart, it's not smart enough to understand
if sys.platform in (...)
checks. So to help it we need to write the code usingor
andelse
instead:Now when running with
win32
platform, mypy ignores theelse
branch and everything is good :)I recommend switching to this, as it removes the need for the
type: ignore
which is better avoided when possible.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.
in that case we might also want report a upstream bug for mypy
after all previously if/else made sense due to win32, now with emscripten, its much more likely to see those in checks
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.
Thank you everyone for being so welcoming and supportive - either in this repo or on LinkedIn. 🙏
And thank you, @bluetech, for taking the time to elaborate and teach me. 🎓💛
Perfect.
I'll do that. I've done that.I'll alsoI've added a comment that gives the reader more type checking context. But I can remove this if the information is obvious to all but the very inexperienced:Outstanding items
1
That sounds like a great idea, @RonnyPfannschmidt.
I'll happily look into how to do that.I've posted to the python/typing gitter with my understanding of the motivation and asked how to officially discuss the code reachability issue.I'll report in when I have been successful or become stuck.2
The in-code comments confused me.
I don't know what we are hoping for or if it is a good idea to! So I'll be looking into that.
At a minimum, I'd like to link to something that gives more context (unless that is a bad idea?).
3I need to figure out how to add the discussed changes in a way that maintains continuity. Am unclear how to do"Add more commits by pushing to the add-type-ignore-for-attr-defined-errors branch on WarrenTheRabbit/pytest."as advised.3
I need to understand
and resolvethecodecov/patch
failure.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 believe it means that the return from
os.getuid()
should always work, but it might return-1
so we account for that in the last line:We should move that comment to above that line. 👍