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

gh-109413: Add a custom script for running mypy on libregrtest #109464

Closed
wants to merge 3 commits into from

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Sep 15, 2023

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.

Copy link
Member Author

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

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
Copy link
Member Author

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

Copy link
Member Author

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) :-)

Copy link
Member Author

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?

  1. Where I have it now?
  2. Inside Lib/test/libregrtest?
  3. Somewhere else?

Also: thoughts on the name? It's a bit long and clunky right now...

Copy link
Member

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.

@hauntsaninja
Copy link
Contributor

(for what it's worth, I think we fixed the Expected type in class pattern; found "Any" diagnostic in python/mypy#14479 )

@AlexWaygood
Copy link
Member Author

(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.

Copy link
Member

@vstinner vstinner left a 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?

Copy link
Member

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.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Sep 17, 2023

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.

@vstinner
Copy link
Member

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()?

@AlexWaygood
Copy link
Member Author

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

@AlexWaygood AlexWaygood deleted the mypy-regrtest-script branch November 30, 2023 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants