GeezerVison is showing github.com — gh-109413: Add a custom script for running mypy on libregrtest by AlexWaygood · Pull Request #109464 · python/cpython
Conversation
AlexWaygood
commented
Sep 15, 2023
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Currently, the only way to run mypy on Lib/test/libregrtest is to cd into Lib/test and then run mypy --config-file libregrtest/mypy.ini. This isn't ideal, as invoking mypy like this means that all things imported from test.support are inferred by mypy as being of type Any. That usually just means that you get some "false negatives" when it comes to type checking, but for us, it also means that we get a false-positive error:
libregrtest\single.py:57: error: Expected type in class pattern; found "Any" [misc]
case TestStats():
^~~~~~~~~~~
This PR adds a custom script for invoking mypy on libregrtest, so that mypy can see the classes and functions inside Lib/test/support/ when it's run on libregrtest. A custom script is needed, as mypy gets very confused generally about the Lib/ directory -- it thinks the whole directory is "shadowing the stdlib". This means that mypy can only be invoked from very specific locations in the CPython repo, or it "sees" everything that's in the Lib/ directory, panics at all the things it doesn't understand, and refuses to do any type-checking at all.
The workaround this script uses is to copy Lib/test/support into a temporary directory and teach mypy about the existence of test.support (without revealing the existence of the rest of the Lib/ directory) using the MYPYPATH environment variable.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Revealing the existence of this module to mypy necessitated fixing a few typing-related things to do with this module
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+1329
to
+1340
|
|
class WarningsPrinter:
|
|
|
def __init__(self, func: Callable[[str], None]) -> None:
|
|
|
self.func = func
|
|
|
# bpo-39983: Store the original sys.stderr at Python startup to be able to
|
|
|
# log warnings even if sys.stderr is captured temporarily by a test.
|
|
|
self.orig_stderr = sys.stderr
|
|
|
|
|
|
def __call__(self, msg: str) -> None:
|
|
|
return self.func(msg)
|
|
|
|
|
|
|
|
|
@WarningsPrinter
|
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.
Mypy doesn't like assigning attributes to functions, unfortunately; it much prefers the idea of a custom callable class
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
@vstinner: where do you think this script should go?
- Where I have it now?
- Inside
Lib/test/libregrtest?
- Somewhere else?
Also: thoughts on the name? It's a bit long and clunky right now...
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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 would prefer to put it in Tools/build/. I would be fine with Lib/test/libregrtest.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
(for what it's worth, I think we fixed the Expected type in class pattern; found "Any" diagnostic in python/mypy#14479 )
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
(for what it's worth, I think we fixed the Expected type in class pattern; found "Any" diagnostic in python/mypy#14479 )
Ah, thank you! I thought I remembered this coming up recently, but I couldn't find the reference when I searched for it.
Hmm, @vstinner, that makes me even less enthused about this approach. I know you said you'd prefer a custom script so that mypy would be able to see the types inside test.support, but to me, this script feels like a hack that's really awkward to invoke. Having everything from test.support being inferred as Any isn't ideal, but it's not the end of the world either, and the only false positive we'd be fixing here is going to go away in an upcoming mypy release anyway. I think the cure might be worse than the disease in this case.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry @AlexWaygood, I'm not sure that I understood the alternative that you are proposing. No script? Can you maybe propose a PR implementating the alternative?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood reacted with thumbs up emoji
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 would prefer to put it in Tools/build/. I would be fine with Lib/test/libregrtest.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood
commented
Sep 17, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Sorry @AlexWaygood, I'm not sure that I understood the alternative that you are proposing. No script? Can you maybe propose a PR implementating the alternative?
Yeah, the alternative would be to just invoke mypy directly by cding into Lib/test and running mypy --config-file libregrtest/mypy.ini -- you can do that now, although there are still some mypy errors. And if #109518 is merged, we won't even need to cd into Lib/test before invoking mypy -- we'll just be able to run mypy --config-file Lib/test/libregrtest/mypy.ini from the repo root to invoke mypy. It feels like a simpler and less hacky approach to me than a custom script.
The only disadvantage from that approach is that everything imported from test.support will be inferred by mypy as having type Any. But that's honestly not the worst thing in the world; I can live with that. There's a bug in mypy 1.5 that means that we would get a single false positive as a result of everything from test.support being inferred as Any. But the bug will be fixed in mypy 1.7 (#109464 (comment)), and in the meantime, we could just # type: ignore it.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
And if #109518 is merged, we won't even need to cd into Lib/test before invoking mypy
It got merged. Do you want to propose an alternative PR which doesn't use temporary directory and copytree()?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
And if #109518 is merged, we won't even need to cd into Lib/test before invoking mypy
It got merged. Do you want to propose an alternative PR which doesn't use temporary directory and copytree()?
yes
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|