Skip to content

Commit 1f26bdc

Browse files
author
Guido van Rossum
committed
Merge upstream/master (464b553)
2 parents 01f3893 + 464b553 commit 1f26bdc

File tree

11 files changed

+114
-17
lines changed

11 files changed

+114
-17
lines changed

docs/source/cheat_sheet.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _cheat-sheet-py2:
22

3-
Mypy syntax cheat sheet (Python 2)
4-
==================================
3+
Type hints cheat sheet (Python 2)
4+
=================================
55

66
This document is a quick cheat sheet showing how the `PEP 484 <https://www.python.org/dev/peps/pep-0484/>`_ type
77
language represents various common types in Python 2.

docs/source/cheat_sheet_py3.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _cheat-sheet-py3:
22

3-
Mypy syntax cheat sheet (Python 3)
4-
==================================
3+
Type hints cheat sheet (Python 3)
4+
=================================
55

66
This document is a quick cheat sheet showing how the `PEP 484 <https://www.python.org/dev/peps/pep-0484/>`_ type
77
language represents various common types in Python 3. Unless otherwise noted, the syntax is valid on all versions of Python 3.

docs/source/index.rst

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,53 @@ Mypy is a static type checker for Python 3 and Python 2.7.
1010

1111
.. toctree::
1212
:maxdepth: 2
13+
:caption: First steps
1314

1415
introduction
1516
basics
1617
getting_started
17-
builtin_types
18+
19+
.. toctree::
20+
:maxdepth: 2
21+
:caption: Cheat sheets
22+
23+
cheat_sheet_py3
24+
cheat_sheet
25+
26+
.. toctree::
27+
:maxdepth: 2
28+
:caption: Type system reference
29+
1830
python2
31+
builtin_types
1932
type_inference_and_annotations
2033
kinds_of_types
2134
class_basics
2235
dynamic_typing
2336
function_overloading
2437
casts
2538
duck_type_compatibility
26-
common_issues
2739
generics
28-
supported_python_features
29-
additional_features
40+
41+
.. toctree::
42+
:maxdepth: 2
43+
:caption: Configuring and running mypy
44+
3045
command_line
3146
config_file
3247
mypy_daemon
48+
supported_python_features
3349
python36
50+
additional_features
51+
52+
53+
.. toctree::
54+
:maxdepth: 2
55+
:caption: Miscellaneous
56+
3457
installed_packages
58+
common_issues
3559
faq
36-
cheat_sheet
37-
cheat_sheet_py3
3860
revision_history
3961

4062
Indices and tables

mypy/nodes.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,18 @@ class SymbolTableNode:
24872487
(This is currently used for simple aliases like `A = int` instead
24882488
of .type_override)
24892489
alias_name: TODO
2490+
no_serialize: Do not serialize this node if True. This is used to prevent
2491+
keys in the cache that refer to modules on which this file does not
2492+
depend. Currently this can happen if there is a module not in build
2493+
used e.g. like this:
2494+
import a.b.c # type: ignore
2495+
This will add a submodule symbol to parent module `a` symbol table,
2496+
but `a.b` is _not_ added as its dependency. Therefore, we should
2497+
not serialize these symbols as they may not be found during fixup
2498+
phase, instead they will be re-added during subsequent patch parents
2499+
phase.
2500+
TODO: Refactor build.py to make dependency tracking more transparent
2501+
and/or refactor look-up functions to not require parent patching.
24902502
"""
24912503

24922504
__slots__ = ('kind',
@@ -2500,6 +2512,7 @@ class SymbolTableNode:
25002512
'implicit',
25012513
'is_aliasing',
25022514
'alias_name',
2515+
'no_serialize',
25032516
)
25042517

25052518
# TODO: This is a mess. Refactor!
@@ -2513,7 +2526,9 @@ def __init__(self,
25132526
normalized: bool = False,
25142527
alias_tvars: Optional[List[str]] = None,
25152528
implicit: bool = False,
2516-
module_hidden: bool = False) -> None:
2529+
module_hidden: bool = False,
2530+
*,
2531+
no_serialize: bool = False) -> None:
25172532
self.kind = kind
25182533
self.node = node
25192534
self.type_override = typ
@@ -2525,6 +2540,7 @@ def __init__(self,
25252540
self.cross_ref = None # type: Optional[str]
25262541
self.is_aliasing = False
25272542
self.alias_name = None # type: Optional[str]
2543+
self.no_serialize = no_serialize
25282544

25292545
@property
25302546
def fullname(self) -> Optional[str]:
@@ -2660,7 +2676,7 @@ def serialize(self, fullname: str) -> JsonDict:
26602676
# module that gets added to every module by
26612677
# SemanticAnalyzerPass2.visit_file(), but it shouldn't be
26622678
# accessed by users of the module.
2663-
if key == '__builtins__':
2679+
if key == '__builtins__' or value.no_serialize:
26642680
continue
26652681
data[key] = value.serialize(fullname, key)
26662682
return data

mypy/semanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,8 @@ def add_submodules_to_parent_modules(self, id: str, module_public: bool) -> None
13011301
child_mod = self.modules.get(id)
13021302
if child_mod:
13031303
sym = SymbolTableNode(MODULE_REF, child_mod,
1304-
module_public=module_public)
1304+
module_public=module_public,
1305+
no_serialize=True)
13051306
parent_mod.names[child] = sym
13061307
id = parent
13071308

mypy/server/aststrip.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ def visit_import(self, node: Import) -> None:
213213
for name, as_name in node.ids:
214214
imported_name = as_name or name
215215
initial = imported_name.split('.')[0]
216-
symnode = self.names[initial]
217-
symnode.kind = UNBOUND_IMPORTED
218-
symnode.node = None
216+
self.names[initial] = SymbolTableNode(UNBOUND_IMPORTED, None)
219217

220218
def visit_import_all(self, node: ImportAll) -> None:
221219
# Imports can include both overriding symbols and fresh ones,

mypy/test/data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ def pytest_addoption(parser: Any) -> None:
578578
group.addoption('--update-data', action='store_true', default=False,
579579
help='Update test data to reflect actual output'
580580
' (supported only for certain tests)')
581+
group.addoption('--mypy-verbose', action='count',
582+
help='Set the verbose flag when creating mypy Options')
581583

582584

583585
# This function name is special to pytest. See

mypy/test/helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ def parse_options(program_text: str, testcase: DataDrivenTestCase,
347347
all(flag not in flag_list for flag in ['--python-version', '-2', '--py2'])):
348348
options.python_version = testcase_pyversion(testcase.file, testcase.name)
349349

350+
if testcase.config.getoption('--mypy-verbose'):
351+
options.verbosity = testcase.config.getoption('--mypy-verbose')
352+
350353
return options
351354

352355

test-data/unit/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ need to pass the `-n0` option to disable parallelization:
171171
You can also write `import pdb; pdb.set_trace()` in code to enter the
172172
debugger.
173173

174+
The `--mypy-verbose` flag can be used to enable additional debug output from
175+
most tests (as if `--verbose` had been passed to mypy):
176+
177+
$ pytest -n0 --mypy-verbose -k MethodCall
174178

175179
Coverage reports
176180
----------------

test-data/unit/check-incremental.test

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4358,10 +4358,27 @@ import b
43584358
[file b.py.2]
43594359
# uh
43604360
-- b gets rechecked because it changed, but nothing is stale
4361-
-- since the interface didn't change
4361+
-- since the interface did not change
43624362
[stale]
43634363
[rechecked b]
43644364

4365+
[case testParentPatchingMess]
4366+
# flags: --ignore-missing-imports --follow-imports=skip
4367+
# cmd: mypy -m d d.k d.k.a d.k.v t
4368+
[file d/__init__.py]
4369+
[file d/k/__init__.py]
4370+
from d.k.a import x
4371+
[file d/k/a.py]
4372+
x = 10
4373+
[file d/k/v.py]
4374+
from d.k.e import x
4375+
4376+
[file t.py]
4377+
from d import k
4378+
[file t.py.2]
4379+
from d import k
4380+
# dummy change
4381+
43654382
[case testBazelFlagIgnoresFileChanges]
43664383
-- Since the initial run wrote a cache file, the second run ignores the source
43674384
# flags: --bazel

0 commit comments

Comments
 (0)