From fef668f264b0db1436a0133edcc4513faf028868 Mon Sep 17 00:00:00 2001 From: Abtin Date: Thu, 12 Mar 2020 12:20:05 +0000 Subject: [PATCH 01/12] Change titles and styling on homepage --- test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000000000..e69de29bb2d1d From ff940ed34b91cea5655e3372fba9b69e8cbbab4c Mon Sep 17 00:00:00 2001 From: Abtin Date: Fri, 13 Mar 2020 22:28:01 +0100 Subject: [PATCH 02/12] Path to source files added in verbose mode Implementation of parts of #6544 , namely of printing the path of the source files given to be processed by mypy. Tested using the command line as well as a configuration file. --- mypy/build.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 330f5cb12e3f8..6b53e30fbefac 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2554,19 +2554,32 @@ def skipping_ancestor(manager: BuildManager, id: str, path: str, ancestor_for: ' "(Using --follow-imports=error, submodule passed on command line)", severity='note', only_once=True) - def log_configuration(manager: BuildManager) -> None: """Output useful configuration information to LOG and TRACE""" manager.log() - configuration_vars = ( + configuration_vars = [ ("Mypy Version", __version__), ("Config File", (manager.options.config_file or "Default")), + ] + + src_pth_str = "Source Path File" + src_pths = manager.source_set.source_paths + + if len(src_pths) > 1: + src_pth_str += "s" + configuration_vars.append((src_pth_str, "")) + for source_path in manager.source_set.source_paths: + configuration_vars.append(("." * len(src_pth_str), source_path)) + else: + configuration_vars.append((src_pth_str, src_pths.pop())) + + configuration_vars.extend([ ("Configured Executable", manager.options.python_executable), ("Current Executable", sys.executable), ("Cache Dir", manager.options.cache_dir), ("Compiled", str(not __file__.endswith(".py"))), - ) + ]) for conf_name, conf_value in configuration_vars: manager.log("{:24}{}".format(conf_name + ":", conf_value)) From 291f17460f5d998003cca9cb96f0e04a8ea17953 Mon Sep 17 00:00:00 2001 From: Abtin Date: Sat, 14 Mar 2020 14:24:25 +0100 Subject: [PATCH 03/12] Fix of failing tests For failing tests of #8536 --- mypy/build.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 6b53e30fbefac..6a85adfc3295c 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2569,10 +2569,12 @@ def log_configuration(manager: BuildManager) -> None: if len(src_pths) > 1: src_pth_str += "s" configuration_vars.append((src_pth_str, "")) - for source_path in manager.source_set.source_paths: + for source_path in src_pths: configuration_vars.append(("." * len(src_pth_str), source_path)) + elif len(src_pths) == 1: + configuration_vars.append((src_pth_str, src_pths.copy().pop())) else: - configuration_vars.append((src_pth_str, src_pths.pop())) + configuration_vars.append((src_pth_str, "None")) configuration_vars.extend([ ("Configured Executable", manager.options.python_executable), From 2f40735d7b6696faf47b90760902f483a509e8a7 Mon Sep 17 00:00:00 2001 From: Abtin Date: Wed, 18 Mar 2020 15:34:53 +0100 Subject: [PATCH 04/12] Fix for failing tests For failing tests of #8536. For the last push I didn't take into account the result of the runtest file which is why 1 test case (for Travis )was failing. --- mypy/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/build.py b/mypy/build.py index 6a85adfc3295c..7010254a24d37 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2577,7 +2577,7 @@ def log_configuration(manager: BuildManager) -> None: configuration_vars.append((src_pth_str, "None")) configuration_vars.extend([ - ("Configured Executable", manager.options.python_executable), + ("Configured Executable", manager.options.python_executable or "None"), ("Current Executable", sys.executable), ("Cache Dir", manager.options.cache_dir), ("Compiled", str(not __file__.endswith(".py"))), From b205a793a5cdd5974fa39e69c3f8f3de935b9505 Mon Sep 17 00:00:00 2001 From: Abtin Date: Wed, 18 Mar 2020 16:28:43 +0100 Subject: [PATCH 05/12] Lint errors fix Lint error fix where blank lines were missing / too many. --- mypy/build.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/build.py b/mypy/build.py index 7010254a24d37..2e97989ac7432 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2554,6 +2554,7 @@ def skipping_ancestor(manager: BuildManager, id: str, path: str, ancestor_for: ' "(Using --follow-imports=error, submodule passed on command line)", severity='note', only_once=True) + def log_configuration(manager: BuildManager) -> None: """Output useful configuration information to LOG and TRACE""" From 731844145ef2915164fc08fd9bcb8e6a39b31c90 Mon Sep 17 00:00:00 2001 From: Abtin Date: Fri, 20 Mar 2020 13:43:31 +0100 Subject: [PATCH 06/12] Changes based on feedback by core team For #8536 feedback of core team leader is applied to code. --- mypy/build.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 2e97989ac7432..737d2a68206c6 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2564,16 +2564,14 @@ def log_configuration(manager: BuildManager) -> None: ("Config File", (manager.options.config_file or "Default")), ] - src_pth_str = "Source Path File" - src_pths = manager.source_set.source_paths + src_pth_str = "Source Path" + src_pths = list(manager.source_set.source_paths.copy()) if len(src_pths) > 1: src_pth_str += "s" - configuration_vars.append((src_pth_str, "")) - for source_path in src_pths: - configuration_vars.append(("." * len(src_pth_str), source_path)) + configuration_vars.append((src_pth_str, " ".join(src_pths))) elif len(src_pths) == 1: - configuration_vars.append((src_pth_str, src_pths.copy().pop())) + configuration_vars.append((src_pth_str, src_pths.pop())) else: configuration_vars.append((src_pth_str, "None")) From 42f3873dd334305e73cc16e701ea12a6ffcb18aa Mon Sep 17 00:00:00 2001 From: abtinf Date: Sat, 21 Mar 2020 12:04:43 +0100 Subject: [PATCH 07/12] Apply sorting of files which are outputted This way the source paths are ordered alphabetically which can be useful if there are many files. --- mypy/build.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/build.py b/mypy/build.py index 737d2a68206c6..fa13bf12b4c3c 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2566,6 +2566,7 @@ def log_configuration(manager: BuildManager) -> None: src_pth_str = "Source Path" src_pths = list(manager.source_set.source_paths.copy()) + src_pths.sort() if len(src_pths) > 1: src_pth_str += "s" From 3256b75a287ad1ede364409e3458844d538b9e5a Mon Sep 17 00:00:00 2001 From: David Zwart Date: Wed, 11 Mar 2020 16:41:11 +0100 Subject: [PATCH 08/12] Add no-site-packages setting Adds main.py logic as well. Tested. --- mypy/config_parser.py | 1 + mypy/main.py | 4 ++-- mypy/options.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 0ee274abd1bcc..ae71d295fab16 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -78,6 +78,7 @@ def split_and_match_files(paths: str) -> List[str]: 'junit_xml': expand_path, # These two are for backwards compatibility 'silent_imports': bool, + 'no_site_packages': bool, 'almost_silent': bool, 'plugins': lambda s: [p.strip() for p in s.split(',')], 'always_true': lambda s: [p.strip() for p in s.split(',')], diff --git a/mypy/main.py b/mypy/main.py index 741527c2ba979..42430967dccd8 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -218,7 +218,7 @@ def infer_python_executable(options: Options, python_executable = special_opts.python_executable or options.python_executable if python_executable is None: - if not special_opts.no_executable: + if not special_opts.no_executable and not options.no_site_packages: python_executable = _python_executable_from_version(options.python_version) options.python_executable = python_executable @@ -839,7 +839,7 @@ def set_strict_flags() -> None: except PythonExecutableInferenceError as e: parser.error(str(e)) - if special_opts.no_executable: + if special_opts.no_executable or options.no_site_packages: options.python_executable = None # Paths listed in the config file will be ignored if any paths are passed on diff --git a/mypy/options.py b/mypy/options.py index 38072b821d156..f3b095d9449c2 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -77,6 +77,7 @@ def __init__(self) -> None: self.report_dirs = {} # type: Dict[str, str] # Show errors in PEP 561 packages/site-packages modules self.no_silence_site_packages = False + self.no_site_packages = False self.ignore_missing_imports = False self.follow_imports = 'normal' # normal|silent|skip|error # Whether to respect the follow_imports setting even for stub files. From 6d012af32646700332c0728bc5931093c038f75a Mon Sep 17 00:00:00 2001 From: David Zwart Date: Thu, 12 Mar 2020 10:04:09 +0100 Subject: [PATCH 09/12] Moved a misplaced key in dict --- mypy/config_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index ae71d295fab16..4721bc0b5f74f 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -78,8 +78,8 @@ def split_and_match_files(paths: str) -> List[str]: 'junit_xml': expand_path, # These two are for backwards compatibility 'silent_imports': bool, - 'no_site_packages': bool, 'almost_silent': bool, + 'no_site_packages': bool, 'plugins': lambda s: [p.strip() for p in s.split(',')], 'always_true': lambda s: [p.strip() for p in s.split(',')], 'always_false': lambda s: [p.strip() for p in s.split(',')], From 3c2cb53b1806b8e07910abe4b79ac84bcbcd142a Mon Sep 17 00:00:00 2001 From: David Zwart Date: Mon, 6 Apr 2020 15:54:08 +0200 Subject: [PATCH 10/12] Added data-driven test --- test-data/unit/pep561.test | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test index acc99c0e797b0..befd365f3dfb7 100644 --- a/test-data/unit/pep561.test +++ b/test-data/unit/pep561.test @@ -7,6 +7,16 @@ reveal_type(a) [out] testTypedPkgSimple.py:5: note: Revealed type is 'builtins.tuple[builtins.str]' +[case testTypedPkg_nositepackages] +# pkgs: typedpkg +# mypy: no-site-packages +from typedpkg.sample import ex +from typedpkg import dne +a = ex(['']) +reveal_type(a) +[out] +testTypedPkg_nositepackages.py:6: note: Revealed type is 'builtins.tuple[builtins.str]' + [case testTypedPkgStubs] # pkgs: typedpkg-stubs from typedpkg.sample import ex From 84c7d42230a6c9e3219d904ac1252e28670b4e62 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Mon, 6 Apr 2020 20:17:34 -0700 Subject: [PATCH 11/12] Add flags and mypy.ini support to test harness --- mypy/test/testpep561.py | 22 ++++++++++++++++++---- test-data/unit/pep561.test | 20 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/mypy/test/testpep561.py b/mypy/test/testpep561.py index bfb8f95ff5abe..a8eabd7702a16 100644 --- a/mypy/test/testpep561.py +++ b/mypy/test/testpep561.py @@ -1,6 +1,7 @@ from contextlib import contextmanager import os import pytest # type: ignore +import re import subprocess from subprocess import PIPE import sys @@ -72,7 +73,7 @@ def install_package(pkg: str, working_dir = os.path.join(package_path, pkg) with tempfile.TemporaryDirectory() as dir: if use_pip: - install_cmd = [python_executable, '-m', 'pip', 'install', '-b {}'.format(dir)] + install_cmd = [python_executable, '-m', 'pip', 'install', '-b', '{}'.format(dir)] if editable: install_cmd.append('-e') install_cmd.append('.') @@ -100,10 +101,11 @@ def test_pep561(testcase: DataDrivenTestCase) -> None: else: python = sys.executable assert python is not None, "Should be impossible" - pkgs, args = parse_pkgs(testcase.input[0]) + pkgs, pip_args = parse_pkgs(testcase.input[0]) + mypy_args = parse_mypy_args(testcase.input[1]) use_pip = True editable = False - for arg in args: + for arg in pip_args: if arg == 'no-pip': use_pip = False elif arg == 'editable': @@ -122,9 +124,14 @@ def test_pep561(testcase: DataDrivenTestCase) -> None: with open(program, 'w', encoding='utf-8') as f: for s in testcase.input: f.write('{}\n'.format(s)) - cmd_line = [program, '--no-incremental', '--no-error-summary'] + cmd_line = mypy_args + [program, '--no-incremental', '--no-error-summary'] if python_executable != sys.executable: cmd_line.append('--python-executable={}'.format(python_executable)) + if testcase.files != []: + for name, content in testcase.files: + if 'mypy.ini' in name: + with open('mypy.ini', 'w') as m: + m.write(content) output = [] # Type check the module out, err, returncode = mypy.api.run(cmd_line) @@ -153,6 +160,13 @@ def parse_pkgs(comment: str) -> Tuple[List[str], List[str]]: return ([pkg.strip() for pkg in pkgs_str.split(',')], [arg.strip() for arg in args]) +def parse_mypy_args(line: str) -> List[str]: + m = re.match('# flags: (.*)$', line) + if not m: + return [] # No args; mypy will spit out an error. + return m.group(1).split() + + @pytest.mark.skipif(sys.platform == 'darwin' and hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix, reason="Temporarily skip to avoid having a virtualenv within a venv.") diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test index befd365f3dfb7..ec82132fd28ac 100644 --- a/test-data/unit/pep561.test +++ b/test-data/unit/pep561.test @@ -1,3 +1,16 @@ +[case testTypedPkgNoSitePkgsIgnoredImports] +# pkgs: typedpkg +# flags: --no-site-packages +from typedpkg.sample import ex +from typedpkg import dne +a = ex(['']) +reveal_type(a) +[file mypy.ini] +\[mypy] +ignore_missing_imports = True +[out] +testTypedPkgNoSitePkgsIgnoredImports.py:6: note: Revealed type is 'Any' + [case testTypedPkgSimple] # pkgs: typedpkg from typedpkg.sample import ex @@ -9,13 +22,16 @@ testTypedPkgSimple.py:5: note: Revealed type is 'builtins.tuple[builtins.str]' [case testTypedPkg_nositepackages] # pkgs: typedpkg -# mypy: no-site-packages +# flags: --no-site-packages from typedpkg.sample import ex from typedpkg import dne a = ex(['']) reveal_type(a) [out] -testTypedPkg_nositepackages.py:6: note: Revealed type is 'builtins.tuple[builtins.str]' +testTypedPkg_nositepackages.py:3: error: Cannot find implementation or library stub for module named 'typedpkg.sample' +testTypedPkg_nositepackages.py:3: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports +testTypedPkg_nositepackages.py:4: error: Cannot find implementation or library stub for module named 'typedpkg' +testTypedPkg_nositepackages.py:6: note: Revealed type is 'Any' [case testTypedPkgStubs] # pkgs: typedpkg-stubs From b08341b228cbcea216e31f1f9d44d9d91a3ab7e1 Mon Sep 17 00:00:00 2001 From: "d.zwart" Date: Tue, 7 Apr 2020 14:47:09 +0200 Subject: [PATCH 12/12] Add 2 tests for no-site-packages --- test-data/unit/pep561.test | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test index ec82132fd28ac..69549b97df462 100644 --- a/test-data/unit/pep561.test +++ b/test-data/unit/pep561.test @@ -20,7 +20,22 @@ reveal_type(a) [out] testTypedPkgSimple.py:5: note: Revealed type is 'builtins.tuple[builtins.str]' -[case testTypedPkg_nositepackages] +[case testTypedPkg_config_nositepackages] +# pkgs: typedpkg +from typedpkg.sample import ex +from typedpkg import dne +a = ex(['']) +reveal_type(a) +[file mypy.ini] +\[mypy] +no_site_packages=True +[out] +testTypedPkg_config_nositepackages.py:2: error: Cannot find implementation or library stub for module named 'typedpkg.sample' +testTypedPkg_config_nositepackages.py:2: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports +testTypedPkg_config_nositepackages.py:3: error: Cannot find implementation or library stub for module named 'typedpkg' +testTypedPkg_config_nositepackages.py:5: note: Revealed type is 'Any' + +[case testTypedPkg_args_nositepackages] # pkgs: typedpkg # flags: --no-site-packages from typedpkg.sample import ex @@ -28,10 +43,10 @@ from typedpkg import dne a = ex(['']) reveal_type(a) [out] -testTypedPkg_nositepackages.py:3: error: Cannot find implementation or library stub for module named 'typedpkg.sample' -testTypedPkg_nositepackages.py:3: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports -testTypedPkg_nositepackages.py:4: error: Cannot find implementation or library stub for module named 'typedpkg' -testTypedPkg_nositepackages.py:6: note: Revealed type is 'Any' +testTypedPkg_args_nositepackages.py:3: error: Cannot find implementation or library stub for module named 'typedpkg.sample' +testTypedPkg_args_nositepackages.py:3: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports +testTypedPkg_args_nositepackages.py:4: error: Cannot find implementation or library stub for module named 'typedpkg' +testTypedPkg_args_nositepackages.py:6: note: Revealed type is 'Any' [case testTypedPkgStubs] # pkgs: typedpkg-stubs