-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[py] make mypy only complain about stuff that matters #10202
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
[py] make mypy only complain about stuff that matters #10202
Conversation
The [NoReturn](https://docs.python.org/3/library/typing.html#typing.NoReturn) type-hint in Python indicates the function never returns (e.g., it always raises an exception). These methods are incorrectly marked as `NoReturn` when they in fact return no value (`None`), and should by hinted using `None`.
…-mypy-only-complain-about-stuff-that-matters
…nly-complain-about-stuff-that-matters
This is an extension of what was done in SeleniumHQ#10120.
Some of the remaining ones might require some refactoring to fix. Perhaps they should just be removed? I feel pretty strongly that incorrect type annotations should never have been here in the first place.
Codecov Report
@@ Coverage Diff @@
## trunk #10202 +/- ##
===========================================
- Coverage 52.24% 26.35% -25.89%
===========================================
Files 84 181 +97
Lines 5482 12896 +7414
Branches 272 209 -63
===========================================
+ Hits 2864 3399 +535
- Misses 2346 9288 +6942
+ Partials 272 209 -63
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
titusfortner
left a comment
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.
This is a lot of work, thank you!
There are a couple browser tests that seem to be flaky in Python suite, but in general CI should be passing, especially the unit tests. Can you fix: https://github.com/SeleniumHQ/selenium/runs/4668655570?check_suite_focus=true#step:7:187
I'm pretty new to typing and mypi, can we surface the issues from PRs in a github action? What do we need to add? https://github.com/SeleniumHQ/selenium/blob/trunk/.github/workflows/python.yml
When you remove all of those checks in mypi.ini what is left, just actual errors? Do we want to create an issue to put those back in after we get things migrated?
oh heck, python 3.7 doesn't have
mypy already runs in CI; to make the job fail when annotations are wrong, you'd need to get rid of the
what's left is errors in things that have explicit annotations; unannotated functions are ignored i'd defer to the mypy docs' recommendations about existing codebases, but that's probably something to play by ear over time; as your typing coverage increases, maybe try turning stuff on occasionally and see how much there is to fix |
3b706c3 to
c0e62b3
Compare
c0e62b3 to
fb2b991
Compare
| """Processes the values that will be typed in the element.""" | ||
| typing: List[str] = [] | ||
| for val in value: | ||
| if isinstance(val, Keys): |
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.
This needs to be reverted. The characters need to be sent one at a time, not as a string, it's causing the 3 interactions errors that include send_keys
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'm confused about how this works, though; Keys is just a class with a bunch of attributes, not an enum or anything, so i'm not sure how this isinstance could ever be truthy before this change
>>> from selenium.webdriver.common.keys import Keys
>>> isinstance(Keys.LEFT, Keys)
Falseif this function needs to be sure that keys are sent one character at a time, it seems to me like it should explicitly check the length of the string, not whether it's an instance of Keys
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 think removing this clause entirely is maybe the right move here
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.
Yeah, I tried adding a few tests and seeing what it does, and I agree, short of passing in Keys() nothing will make it true. :)
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 think this is resolvable, too, if you agree with the change i made
The if case that was here before this PR existed required someone to hand in an instance of Keys, which I don't think was ever happening. Here, then, we just remove the case entirely and fall back to iterating across any string, even ones of length 1.
1099158 to
5b1d1d1
Compare
| self._execute(Command.SEND_KEYS_TO_ELEMENT, | ||
| {'text': "".join(keys_to_typing(value)), | ||
| 'value': keys_to_typing(value)}) | ||
| {'text': "".join(keys_to_typing(output)), |
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.
This is causing test-remote execution to fail: https://github.com/SeleniumHQ/selenium/runs/4708398183
It looks like there's an UnboundLocalError issue because output isn't defined for condition: None in local_files
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 think i've fixed this in 41b47ea, but i'm a bit confused about this method in general; it seems like this function will perform dramatically different things based on whether one entry in a list of filenames is not a local file, and now that i know this machinery is here, i'm worried about accidentally matching file names from my current working directory and accidentally sending their contents to my test runners
i guess it probably doesn't matter that much, since it still sends the same data to the actual field, but it certainly feels like an unexpected interaction to happen, which scares me when that action is sending the contents of local files over a network. also, debugging a test failure due to this behaviour not happening when you expected it to seems like it'd potentially be pretty difficult
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.
So, this has to do with file uploads. If the element is a file type input element and the string passed in send keys is a path to a file, the driver will handle it as an upload.
Look at Point 8 here for remote end steps for element send keys:
https://w3c.github.io/webdriver/#:~:text=the%20remote%20end%20steps%20for%20element%20send%20keys%20are
This gets weird when using a remote driver and the file is on a different system than the driver, which requires using a local file detector and uploading the file to the remote machine
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 figured as much, but if that's the case, i feel like this should check whether the field is a file input and then throw an error if one of the files doesn't exist, so that it's predictable and deterministic, instead of using this heuristic based on the local filesystem, and [removed because i didn't understand what "remote end" means]
...i still feel like this is a dangerous and surprising piece of behaviour, but i guess i'd have to raise this against the spec itself, and it doesn't have anything to do with this PR other than that i broke it
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 haven't dug into the Python implementation. I'm wondering if it might make sense to have new methods in the bindings specifically for "upload" so we aren't overloading "send keys," but we're also looking at making a separate upload endpoint in w3c, so maybe that should be done first in case the user-facing implementation in the bindings is going to be different.
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 still don't think there's any action left to be taken here as part of this PR
It can't serialize them.
Wow, I really wasn't thorough with these at all. Sorry everyone.
The tests require it, but these methods now naturally throw AttributeError in these cases instead.
Also, annotate these tests in the hopes of catching similar stuff.
|
@symonk :) (i don't think the remaining firefox failures are this branch's faulr) |
It was weird having a few tests here that seemed to do more for no reason.
|
@colons apologies for the lack of feedback. Wondering if you are still interested in this PR? If so, I can help to move it forward and review. |
|
Here some of my thoughts: |
|
I am sorry about this. I am going to close this PR. It has not had activity in a long time, and the code base has moved, which is causing conflicts. We are open to receiving more PRs about this, and hopefully, they are small, so they can get merged quickly. |
Description
This PR disables the mypy checks that don't matter for a codebase that is only beginning to adopt type checks, in the hopes that flagrantly incorrect annotations such as the ones fixed in #10120 and #10172 can be avoided. It also fixes a number of other very wrong annotations,
although not all of them. Some would be hard to fix without removing them entirely, refactoring, or breaking backwards compatibility, and I did not want to do that here without raising it first.and adds a now-passing mypy check to CI.Byhas been turned into anEnum, although methods which accepted string representations of locator strategies still do, and the tests have been expanded to guarantee that.Keysis probably worth treating the same, but I have not done that here.I have added more annotations in places where doing so helped with testing. I will probably have more PRs in the near future, in particular to annotate the tests.
The only remaining CI failures appear to be also happening in
trunk.Also, one of the things mypy is complaining about is thatraised in #10210WheelInputis given too many arguments inaction_builder.ActionBuilder.add_wheel_input, which I'm confused about. Is there some magic I'm missing, or is this just a method that isn't working at the moment?Motivation and Context
I observed in a comment that the errors that have been made in the existing annotations are completely understandable given how overwhelmingly noisy the current mypy checks are. In an existing codebase, complaining about stuff like missing return annotations is pointless; all you really want to do is add annotations as you work on stuff and make sure that what you do add is correct.
Incorrect annotations are way, way worse than no annotations.
Types of changes
Checklist