Skip to content

[stubgen] Add plugin support by passing a configuration file (#14428) #14436

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import mypy.traverser
import mypy.util
from mypy.build import build
from mypy.config_parser import parse_config_file
from mypy.errors import CompileError, Errors
from mypy.find_sources import InvalidSourceList, create_source_list
from mypy.modulefinder import (
Expand Down Expand Up @@ -229,6 +230,7 @@ def __init__(
verbose: bool,
quiet: bool,
export_less: bool,
mypy_config_file: str,
) -> None:
# See parse_options for descriptions of the flags.
self.pyversion = pyversion
Expand All @@ -247,6 +249,7 @@ def __init__(
self.verbose = verbose
self.quiet = quiet
self.export_less = export_less
self.mypy_config_file = mypy_config_file


class StubSource:
Expand Down Expand Up @@ -1568,6 +1571,14 @@ def mypy_options(stubgen_options: Options) -> MypyOptions:
options.python_version = stubgen_options.pyversion
options.show_traceback = True
options.transform_source = remove_misplaced_type_comments
if stubgen_options.mypy_config_file:

def set_strict_flags() -> None: # not needed yet
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that this is correct. It would treat strict differently.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with this callback so I had just replicated #9203
We can instead replicate main.py and do

    def set_strict_flags() -> None:
        for dest, value in strict_flag_assignments:
            setattr(options, dest, value)

would that work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, something like this would work (I hope so, at least)!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I'm not familiar enough to know how to continue. The blurb I linked refers to a strict_flag_assignments variable that gets populated through some deeper method. Do you think strict flags are something that would be useful when parsing config files in stubgen.py? (I'm not sure of what strict flags even are, maybe with some extra context I can help implement something more robust)

Otherwise, would you be ok leaving the current implementation as is (it at least matches the existing [already merged] stubtest.py PR that inspired my PR) and someone more knowledgeable could propose a better handling of strict flags in the future?

Thanks for your quick response!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if this is hard to do, we can skip it for now :)


parse_config_file(
options, set_strict_flags, stubgen_options.mypy_config_file, sys.stdout, sys.stderr
)
return options


Expand Down Expand Up @@ -1810,6 +1821,15 @@ def parse_options(args: list[str]) -> Options:
dest="files",
help="generate stubs for given files or directories",
)
config_group = parser.add_argument_group(
title="mypy config file",
description="Use a config file to override command line arguments",
)
config_group.add_argument(
"--mypy-config-file",
default="",
help="An existing mypy configuration file (e.g. defining additional plugins)",
)

ns = parser.parse_args(args)

Expand Down Expand Up @@ -1841,6 +1861,7 @@ def parse_options(args: list[str]) -> Options:
verbose=ns.verbose,
quiet=ns.quiet,
export_less=ns.export_less,
mypy_config_file=ns.mypy_config_file,
)


Expand Down