Skip to content

add modules and packages keys to config #9891

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ section of the command line docs.

This option may only be set in the global section (``[mypy]``).

.. confval:: modules

:type: comma-separated list of strings

A comma-separated list of modules to be type checked. See the
corresponding flag :option:`--module <mypy --module>` for more information.

This option may only be set in the global section (``[mypy]``).

.. confval:: packages

:type: comma-separated list of strings

A comma-separated list of packages to be type checked. See the
corresponding flag :option:`--package <mypy --package>` for more information.

This option may only be set in the global section (``[mypy]``).

.. confval:: exclude

:type: newline separated list of regular expressions
Expand Down
8 changes: 8 additions & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def check_follow_imports(choice: str) -> str:
'custom_typeshed_dir': expand_path,
'mypy_path': lambda s: [expand_path(p.strip()) for p in re.split('[,:]', s)],
'files': split_and_match_files,
'packages': lambda s: [p.strip() for p in s.split(',')],
'modules': lambda s: [p.strip() for p in s.split(',')],
'quickstart_file': expand_path,
'junit_xml': expand_path,
# These two are for backwards compatibility
Expand Down Expand Up @@ -426,6 +428,12 @@ def parse_section(prefix: str, template: Options,
if 'follow_imports' not in results:
results['follow_imports'] = 'error'
results[options_key] = v
if key in ('files', 'packages', 'modules'):
if (('packages' in results or 'modules' in results) and results.get('files')):
print("May only specify one of: module/package or files. Ignoring key:", key,
file=stderr)
del results[options_key]
continue
return results, report_dirs


Expand Down
9 changes: 7 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,13 @@ def set_strict_flags() -> None:

# Paths listed in the config file will be ignored if any paths, modules or packages
# are passed on the command line.
if options.files and not (special_opts.files or special_opts.packages or special_opts.modules):
special_opts.files = options.files
if not (special_opts.files or special_opts.packages or special_opts.modules):
if options.files:
special_opts.files = options.files
if options.modules:
special_opts.modules = options.modules
if options.packages:
special_opts.packages = options.packages

# Check for invalid argument combinations.
if require_targets:
Expand Down
6 changes: 6 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def __init__(self) -> None:
# supports globbing
self.files: Optional[List[str]] = None

# A comma-separated list of packages for mypy to type check;
self.packages = None # type: Optional[List[str]]

# A comma-separated list of modules for mypy to type check;
self.modules = None # type: Optional[List[str]]

# Write junit.xml to given file
self.junit_xml: Optional[str] = None

Expand Down
54 changes: 53 additions & 1 deletion test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,59 @@ p/a.py:4: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
p/b/__init__.py:5: error: Argument 1 to "bar" has incompatible type "str"; expected "int"
c.py:2: error: Argument 1 to "bar" has incompatible type "str"; expected "int"

[case testModulesAndPackagesInIni]
# cmd: mypy
[file mypy.ini]
\[mypy]
modules=c
packages=p.a,p.b
[file p/__init__.py]
[file p/a.py]
def foo(x):
# type: (int) -> str
return "x"
foo("wrong")
[file p/b/__init__.py]
from ..a import foo
def bar(a):
# type: (int) -> str
return foo(a)
bar("wrong")
[file c.py]
import p.b
p.b.bar("wrong")
[out]
p/a.py:4: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
p/b/__init__.py:5: error: Argument 1 to "bar" has incompatible type "str"; expected "int"
c.py:2: error: Argument 1 to "bar" has incompatible type "str"; expected "int"

[case testModulesAndPackagesAndFilesInIni]
# cmd: mypy
[file mypy.ini]
\[mypy]
modules=c
packages=p.a,p.b
files=meh.py
[out]
May only specify one of: module/package or files. Ignoring key: files
Can't find package 'p.a'
== Return code: 2


[case testModulesAndPackagesAndFilesInIniModLater]
# cmd: mypy
[file mypy.ini]
\[mypy]
files=meh.py
modules=c
packages=p.a,p.b
[out]
May only specify one of: module/package or files. Ignoring key: modules
May only specify one of: module/package or files. Ignoring key: packages
mypy: cannot read file 'meh.py': No such file or directory
== Return code: 2


[case testSrcPEP420Packages]
# cmd: mypy -p anamespace --namespace-packages
[file mypy.ini]
Expand Down Expand Up @@ -1271,7 +1324,6 @@ y = 0 # type: str
[out]
pkg.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")


[case testCmdlineModuleAndIniFiles]
# cmd: mypy -m pkg
[file mypy.ini]
Expand Down