-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Adding config file for stubtest.py #9203
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
Adding config file for stubtest.py #9203
Conversation
mypy/stubtest.py
Outdated
|
||
plugins = [] | ||
if options.config_file: | ||
def set_strict_flags() -> None: # not needed yet |
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.
not sure if there's a better way to deal with this - seems like we just don't really need that param but I didn't want to pass in lambda _: None
. open to suggestions here
@@ -1133,6 +1150,15 @@ def parse_options(args: List[str]) -> argparse.Namespace: | |||
action="store_true", | |||
help="Ignore unused whitelist entries", | |||
) | |||
config_group = parser.add_argument_group( |
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.
copied directly from main.py
.
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.
modified the description, though
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 making this happen! You'll want to address the CI complaints.
It would be good to add a test, maybe using a simple get_type_analyze_hook
plugin.
As I mentioned in the issue, I think I'd prefer not reusing mypy's config. stubtest and mypy are pretty different tools and this could cause problems with users' expectations. For instance, one might expect to be able to set --ignore-positional-only in the config.
The fact that we're ignoring most of the config could also lead to surprises in the other direction. For instance, if I'd used this approach initially (but not wired up the plugins code), it would have been harder for you to see that stubtest didn't support plugins.
It's also worth mentioning that I'm not a maintainer, so my opinions only count for so much, plus I don't have the ability to merge.
@hauntsaninja Thanks for the comment. I don't have a great idea of all the cases where you want to use different settings (not just a subset, but actually different). If there are a bunch, then I agree we should somehow make it clear that this needs a different config. I wonder if there are other ways we could make it more clear? Maybe we rename the arg to In my use case, it's nice to have the config file because it automatically picks up on all of the following settings that we want to be the same:
|
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.
Hmm, I hadn't thought of the fact that plugins set settings in mypy's config file as well (not a big plugin user myself). Renaming the option is a good idea then (maybe --mypy-config-file
).
mypy/stubtest.py
Outdated
def set_strict_flags() -> None: # not needed yet | ||
return | ||
parse_config_file(options, set_strict_flags, options.config_file, sys.stdout, sys.stderr) | ||
errors = Errors(options.show_error_context, |
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.
Since we don't ever set any of these options, the defaults match the defaults of Options, and because pretty is False we never use read_source, maybe we can just pass in Errors()
?
That way you also no longer need to worry about cached_read etc not existing
edit: I'm a dummy, I forgot about the config file, lol
mypy/stubtest.py
Outdated
config_group.add_argument( | ||
'--config-file', | ||
help="Configuration file, must have a [mypy] section " | ||
"(defaults to {})".format(', '.join(defaults.CONFIG_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.
We don't actually set this default anywhere (and maybe we shouldn't?)
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, this is much cleaner! This looks good to me; if you're willing to add a test, that would be the only improvement I could suggest :-)
mypy/stubtest.py
Outdated
config_group = parser.add_argument_group( | ||
title='mypy config file', | ||
description="Use a config file instead of command line arguments. " | ||
"Plugins and mypy path are the only supported " | ||
"configurations.", | ||
) | ||
config_group.add_argument( | ||
'--mypy-config-file', | ||
help="Configuration file, must have a [mypy] section", | ||
) |
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.
minor: a wording suggestion
config_group = parser.add_argument_group( | |
title='mypy config file', | |
description="Use a config file instead of command line arguments. " | |
"Plugins and mypy path are the only supported " | |
"configurations.", | |
) | |
config_group.add_argument( | |
'--mypy-config-file', | |
help="Configuration file, must have a [mypy] section", | |
) | |
config_group.add_argument( | |
'--mypy-config-file', | |
help="An existing mypy configuration file, currently used by stubtest to help determine mypy path and plugins", | |
) |
@hauntsaninja Could you elaborate on how this test would work? I spent some time today trying to get one running with
If you take a look at named_callable.py, it looks for a I tried similar approaches with Is there an example test case you could point me to? Thanks for all your help! |
stubtest uses mypy to build the stubs. The runtime is only imported and introspected using |
Thanks, looks good! |
This fixes 9193.
Before: You could not use plugins during the stub loading step.
Problem: If you need plugins for regular static type checking, you may need them when loading stubs for runtime type checking as well.
Solution: Add support for loading plugins from a config file. The easiest way to do this is to just load the config file and let
build
take care of it.