| 1 |
-- Tests for command line parsing |
| 2 |
-- ------------------------------ |
| 3 |
-- |
| 4 |
-- The initial line specifies the command line, in the format |
| 5 |
-- |
| 6 |
-- # cmd: mypy <options> |
| 7 |
-- |
| 8 |
-- Note that # flags: --some-flag IS NOT SUPPORTED. |
| 9 |
-- Use # cmd: mypy --some-flag ... |
| 10 |
-- |
| 11 |
-- '== Return code: <value>' is added to the output when the process return code |
| 12 |
-- is "nonobvious" -- that is, when it is something other than 0 if there are no |
| 13 |
-- messages and 1 if there are. |
| 14 |
|
| 15 |
-- Directories/packages on the command line |
| 16 |
-- ---------------------------------------- |
| 17 |
|
| 18 |
[case testNonArrayOverridesPyprojectTOML] |
| 19 |
# cmd: mypy x.py |
| 20 |
[file pyproject.toml] |
| 21 |
\[tool.mypy] |
| 22 |
\[tool.mypy.overrides] |
| 23 |
module = "x" |
| 24 |
disallow_untyped_defs = false |
| 25 |
[file x.py] |
| 26 |
def f(a): |
| 27 |
pass |
| 28 |
def g(a: int) -> int: |
| 29 |
return f(a) |
| 30 |
[out] |
| 31 |
pyproject.toml: tool.mypy.overrides sections must be an array. Please make sure you are using double brackets like so: [[tool.mypy.overrides]] |
| 32 |
== Return code: 0 |
| 33 |
|
| 34 |
[case testNoModuleInOverridePyprojectTOML] |
| 35 |
# cmd: mypy x.py |
| 36 |
[file pyproject.toml] |
| 37 |
\[tool.mypy] |
| 38 |
\[[tool.mypy.overrides]] |
| 39 |
disallow_untyped_defs = false |
| 40 |
[file x.py] |
| 41 |
def f(a): |
| 42 |
pass |
| 43 |
def g(a: int) -> int: |
| 44 |
return f(a) |
| 45 |
[out] |
| 46 |
pyproject.toml: toml config file contains a [[tool.mypy.overrides]] section, but no module to override was specified. |
| 47 |
== Return code: 0 |
| 48 |
|
| 49 |
[case testInvalidModuleInOverridePyprojectTOML] |
| 50 |
# cmd: mypy x.py |
| 51 |
[file pyproject.toml] |
| 52 |
\[tool.mypy] |
| 53 |
\[[tool.mypy.overrides]] |
| 54 |
module = 0 |
| 55 |
disallow_untyped_defs = false |
| 56 |
[file x.py] |
| 57 |
def f(a): |
| 58 |
pass |
| 59 |
def g(a: int) -> int: |
| 60 |
return f(a) |
| 61 |
[out] |
| 62 |
pyproject.toml: toml config file contains a [[tool.mypy.overrides]] section with a module value that is not a string or a list of strings |
| 63 |
== Return code: 0 |
| 64 |
|
| 65 |
[case testConflictingModuleInOverridesPyprojectTOML] |
| 66 |
# cmd: mypy x.py |
| 67 |
[file pyproject.toml] |
| 68 |
\[tool.mypy] |
| 69 |
\[[tool.mypy.overrides]] |
| 70 |
module = 'x' |
| 71 |
disallow_untyped_defs = false |
| 72 |
\[[tool.mypy.overrides]] |
| 73 |
module = ['x'] |
| 74 |
disallow_untyped_defs = true |
| 75 |
[file x.py] |
| 76 |
def f(a): |
| 77 |
pass |
| 78 |
def g(a: int) -> int: |
| 79 |
return f(a) |
| 80 |
[out] |
| 81 |
pyproject.toml: toml config file contains [[tool.mypy.overrides]] sections with conflicting values. Module 'x' has two different values for 'disallow_untyped_defs' |
| 82 |
== Return code: 0 |
| 83 |
|
| 84 |
[case testMultilineLiteralExcludePyprojectTOML] |
| 85 |
# cmd: mypy x |
| 86 |
[file pyproject.toml] |
| 87 |
\[tool.mypy] |
| 88 |
exclude = '''(?x)( |
| 89 |
(^|/)[^/]*skipme_\.py$ |
| 90 |
|(^|/)_skipme[^/]*\.py$ |
| 91 |
)''' |
| 92 |
[file x/__init__.py] |
| 93 |
i: int = 0 |
| 94 |
[file x/_skipme_please.py] |
| 95 |
This isn't even syntactically valid! |
| 96 |
[file x/please_skipme_.py] |
| 97 |
Neither is this! |
| 98 |
|
| 99 |
[case testMultilineBasicExcludePyprojectTOML] |
| 100 |
# cmd: mypy x |
| 101 |
[file pyproject.toml] |
| 102 |
\[tool.mypy] |
| 103 |
exclude = """(?x)( |
| 104 |
(^|/)[^/]*skipme_\\.py$ |
| 105 |
|(^|/)_skipme[^/]*\\.py$ |
| 106 |
)""" |
| 107 |
[file x/__init__.py] |
| 108 |
i: int = 0 |
| 109 |
[file x/_skipme_please.py] |
| 110 |
This isn't even syntactically valid! |
| 111 |
[file x/please_skipme_.py] |
| 112 |
Neither is this! |
| 113 |
|
| 114 |
[case testSequenceExcludePyprojectTOML] |
| 115 |
# cmd: mypy x |
| 116 |
[file pyproject.toml] |
| 117 |
\[tool.mypy] |
| 118 |
exclude = [ |
| 119 |
'(^|/)[^/]*skipme_\.py$', # literal (no escaping) |
| 120 |
"(^|/)_skipme[^/]*\\.py$", # basic (backslash needs escaping) |
| 121 |
] |
| 122 |
[file x/__init__.py] |
| 123 |
i: int = 0 |
| 124 |
[file x/_skipme_please.py] |
| 125 |
This isn't even syntactically valid! |
| 126 |
[file x/please_skipme_.py] |
| 127 |
Neither is this! |
| 128 |
|
| 129 |
[case testPyprojectTOMLUnicode] |
| 130 |
# cmd: mypy x.py |
| 131 |
[file pyproject.toml] |
| 132 |
\[project] |
| 133 |
description = "Factory ⸻ A code generator 🏭" |
| 134 |
\[tool.mypy] |
| 135 |
[file x.py] |
| 136 |
|
| 137 |
[case testPyprojectFilesTrailingComma] |
| 138 |
# cmd: mypy |
| 139 |
[file pyproject.toml] |
| 140 |
\[tool.mypy] |
| 141 |
# We combine multiple tests in a single one here, because these tests are slow. |
| 142 |
files = """ |
| 143 |
a.py, |
| 144 |
b.py, |
| 145 |
""" |
| 146 |
always_true = """ |
| 147 |
FLAG_A1, |
| 148 |
FLAG_B1, |
| 149 |
""" |
| 150 |
always_false = """ |
| 151 |
FLAG_A2, |
| 152 |
FLAG_B2, |
| 153 |
""" |
| 154 |
[file a.py] |
| 155 |
x: str = 'x' # ok' |
| 156 |
|
| 157 |
# --always-true |
| 158 |
FLAG_A1 = False |
| 159 |
FLAG_B1 = False |
| 160 |
if not FLAG_A1: # unreachable |
| 161 |
x: int = 'x' |
| 162 |
if not FLAG_B1: # unreachable |
| 163 |
y: int = 'y' |
| 164 |
|
| 165 |
# --always-false |
| 166 |
FLAG_A2 = True |
| 167 |
FLAG_B2 = True |
| 168 |
if FLAG_A2: # unreachable |
| 169 |
x: int = 'x' |
| 170 |
if FLAG_B2: # unreachable |
| 171 |
y: int = 'y' |
| 172 |
[file b.py] |
| 173 |
y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 174 |
[file c.py] |
| 175 |
# This should not trigger any errors, because it is not included: |
| 176 |
z: int = 'z' |
| 177 |
[out] |
| 178 |
|
| 179 |
[case testPyprojectModulesTrailingComma] |
| 180 |
# cmd: mypy |
| 181 |
[file pyproject.toml] |
| 182 |
\[tool.mypy] |
| 183 |
# We combine multiple tests in a single one here, because these tests are slow. |
| 184 |
modules = """ |
| 185 |
a, |
| 186 |
b, |
| 187 |
""" |
| 188 |
disable_error_code = """ |
| 189 |
operator, |
| 190 |
import, |
| 191 |
""" |
| 192 |
enable_error_code = """ |
| 193 |
redundant-expr, |
| 194 |
ignore-without-code, |
| 195 |
""" |
| 196 |
[file a.py] |
| 197 |
x: str = 'x' # ok |
| 198 |
|
| 199 |
# --enable-error-code |
| 200 |
a: int = 'a' # type: ignore |
| 201 |
|
| 202 |
# --disable-error-code |
| 203 |
'a' + 1 |
| 204 |
[file b.py] |
| 205 |
y: int = 'y' |
| 206 |
[file c.py] |
| 207 |
# This should not trigger any errors, because it is not included: |
| 208 |
z: int = 'z' |
| 209 |
[out] |
| 210 |
b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 211 |
a.py:4: error: "type: ignore" comment without error code (consider "type: ignore[assignment]" instead) |
| 212 |
|
| 213 |
[case testPyprojectPackagesTrailingComma] |
| 214 |
# cmd: mypy |
| 215 |
[file pyproject.toml] |
| 216 |
\[tool.mypy] |
| 217 |
packages = """ |
| 218 |
a, |
| 219 |
b, |
| 220 |
""" |
| 221 |
[file a/__init__.py] |
| 222 |
x: str = 'x' # ok |
| 223 |
[file b/__init__.py] |
| 224 |
y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 225 |
[file c/__init__.py] |
| 226 |
# This should not trigger any errors, because it is not included: |
| 227 |
z: int = 'z' |
| 228 |
[out] |
| 229 |
|
| 230 |
[case testPyprojectTOMLSettingOfWrongType] |
| 231 |
# cmd: mypy a.py |
| 232 |
[file pyproject.toml] |
| 233 |
\[tool.mypy] |
| 234 |
enable_error_code = true |
| 235 |
[file a.py] |
| 236 |
x: int = 1 |
| 237 |
[out] |
| 238 |
pyproject.toml: [mypy]: enable_error_code: Expected a list or a stringified version thereof, but got: 'True', of type bool. |
| 239 |
== Return code: 0 |