-
-
Notifications
You must be signed in to change notification settings - Fork 30.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
gh-109413: Add a custom script for running mypy on libregrtest #109464
Conversation
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
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
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 the solution you said you preferred in #109382 (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.
@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...
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.
(for what it's worth, I think we fixed the |
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 |
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?
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.
Yeah, the alternative would be to just invoke mypy directly by The only disadvantage from that approach is that everything imported from |
It got merged. Do you want to propose an alternative PR which doesn't use temporary directory and copytree()? |
yes |
Currently, the only way to run mypy on
Lib/test/libregrtest
is tocd
intoLib/test
and then runmypy --config-file libregrtest/mypy.ini
. This isn't ideal, as invoking mypy like this means that all things imported fromtest.support
are inferred by mypy as being of typeAny
. 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: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 theLib/
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 theLib/
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 oftest.support
(without revealing the existence of the rest of theLib/
directory) using theMYPYPATH
environment variable.