Skip to content

Commit e03f41d

Browse files
authored
Use OrderedDict for raw per-module options to preserve section order (#3678)
Fixes #3675. Note that the problem only occurs on Python 3.5 and earlier, since in Python 3.6 the standard dict type preserves order. Also note that this relies on ConfigParser preserving order -- which it does (it uses OrderedDict for everything by default).
1 parent 108f714 commit e03f41d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

docs/source/config_file.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ overridden by the pattern sections matching the module name.
124124
.. note::
125125

126126
If multiple pattern sections match a module they are processed in
127-
unspecified order.
127+
order of their occurrence in the config file.
128128

129129
- ``follow_imports`` (string, default ``normal``) directs what to do
130130
with imports when the imported module is found as a ``.py`` file and

mypy/options.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from collections import OrderedDict
12
import fnmatch
23
import pprint
34
import sys
45

5-
from typing import Mapping, Optional, Tuple, List, Pattern, Dict
6+
from typing import Mapping, MutableMapping, Optional, Tuple, List, Pattern, Dict
67

78
from mypy import defaults
89

@@ -119,7 +120,8 @@ def __init__(self) -> None:
119120
self.plugins = [] # type: List[str]
120121

121122
# Per-module options (raw)
122-
self.per_module_options = {} # type: Dict[Pattern[str], Dict[str, object]]
123+
pm_opts = OrderedDict() # type: OrderedDict[Pattern[str], Dict[str, object]]
124+
self.per_module_options = pm_opts
123125

124126
# -- development options --
125127
self.verbosity = 0 # More verbose messages (for troubleshooting)

test-data/unit/cmdline.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,26 @@ m.py:4: error: Missing type parameters for generic type
10311031
m.py:5: error: Missing type parameters for generic type
10321032
m.py:6: error: Missing type parameters for generic type
10331033
m.py:7: error: Missing type parameters for generic type
1034+
1035+
[case testDeterministicSectionOrdering]
1036+
# cmd: mypy a
1037+
[file a/__init__.py]
1038+
[file a/b/__init__.py]
1039+
[file a/b/c/__init__.py]
1040+
[file a/b/c/d/__init__.py]
1041+
[file a/b/c/d/e/__init__.py]
1042+
0()
1043+
[file mypy.ini]
1044+
[[mypy]
1045+
[[mypy-a.*]
1046+
ignore_errors = True
1047+
[[mypy-a.b.*]
1048+
ignore_errors = True
1049+
[[mypy-a.b.c.*]
1050+
ignore_errors = True
1051+
[[mypy-a.b.c.d.*]
1052+
ignore_errors = True
1053+
[[mypy-a.b.c.d.e]
1054+
ignore_errors = False
1055+
[out]
1056+
a/b/c/d/e/__init__.py:1: error: "int" not callable

0 commit comments

Comments
 (0)