Skip to content

Commit 31e508f

Browse files
Write some tests that enforce that disabled_error_codes and enabled_error_codes do not affect program behavior
While disabled_error_codes and enabled_error_codes are internal fields in the options class, they cannot be set externally, unlike their peers. These tests ensure this intended behavior is maintained. Closes #12394, which failed to understand this. Some of these tests have to be in cmdline.test because they error so badly that they break check-flags.test (they exit with 2 and print to stderr instead, which apparently check-flags.test can't handle; cf #19554).
1 parent 88a3c58 commit 31e508f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

test-data/unit/check-flags.test

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
[case testDisabledErrorCodesIsNotARealFlagContrast]
2+
-- This is the real flag, that contrasts with the fake one in the other test.
3+
-- see also: cmdline.test for the one that does a usage error and prints to stderr
4+
# flags: --disable-error-code=name-defined
5+
A
6+
7+
[case testDisabledErrorCodesIsNotARealFlagConfig]
8+
-- disabled-error-codes is not a real flag, just an implementation field, and this test serves to ensure it doesn't affect program behavior.
9+
# flags: --config tmp/n.ini
10+
A # E: Name "A" is not defined
11+
[file n.ini]
12+
\[mypy]
13+
disabled_error_codes=name-defined
14+
15+
[case testDisabledErrorCodesIsNotARealFlagInline]
16+
-- disabled-error-codes is not a real flag, just an implementation field, and this test serves to ensure it doesn't affect program behavior.
17+
#mypy: disabled_error_codes=name-defined
18+
A # E: Name "A" is not defined
19+
20+
21+
[case testEnabledErrorCodesIsNotARealFlagContrast]
22+
-- This is the real flag, that contrasts with the fake one in the other test.
23+
-- see also: cmdline.test for the one that does a usage error and prints to stderr
24+
# flags: --enable-error-code=unused-ignore
25+
#type: ignore # E: Unused "type: ignore" comment
26+
27+
[case testEnabledErrorCodesIsNotARealFlagConfig]
28+
-- enabled-error-codes is not a real flag, just an implementation field, and this test serves to ensure it doesn't affect program behavior.
29+
# flags: --config tmp/n.ini
30+
#type: ignore
31+
[file n.ini]
32+
\[mypy]
33+
disabled_error_codes=unused-ignore
34+
35+
[case testEnabledErrorCodesIsNotARealFlagInline]
36+
-- enabled-error-codes is not a real flag, just an implementation field, and this test serves to ensure it doesn't affect program behavior.
37+
#mypy: enabled_error_codes=unused-ignore
38+
#type: ignore
39+
40+
141
[case testUnannotatedFunction]
242
# flags: --disallow-untyped-defs
343
def f(x): pass

test-data/unit/cmdline.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,41 @@ Warning: TypeVarTuple is already enabled by default
13881388
Warning: Unpack is already enabled by default
13891389
== Return code: 0
13901390

1391+
[case testDisabledErrorCodesIsNotARealFlagCmdContrast]
1392+
# cmd: mypy --disable-error-code=name-defined a.py
1393+
-- This is the real flag, that contrasts with the fake one in the other test.
1394+
[file a.py]
1395+
A
1396+
1397+
[case testDisabledErrorCodesIsNotARealFlagCmd]
1398+
# cmd: mypy --disabled-error-codes=name-defined a.py
1399+
-- disabled-error-codes is not a real flag, just an implementation field, and this test serves to ensure it doesn't affect program behavior.
1400+
-- see also: check-flags.test for the ones that can be check without the full cmdline.test treatment.
1401+
[file a.py]
1402+
A
1403+
[out]
1404+
usage: mypy [-h] [-v] [-V] [more options; see below]
1405+
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
1406+
mypy: error: unrecognized arguments: --disabled-error-codes=name-defined
1407+
== Return code: 2
1408+
1409+
[case testEnabledErrorCodesIsNotARealFlagCmdContrast]
1410+
# cmd: mypy --enable-error-code=unused-ignore a.py
1411+
-- This is the real flag, that contrasts with the fake one in the other test.
1412+
[file a.py]
1413+
#type: ignore # E: Unused "type: ignore" comment
1414+
1415+
[case testEnabledErrorCodesIsNotARealFlagCmd]
1416+
# cmd: mypy --enabled-error-codes=unused-ignore a.py
1417+
-- enabled-error-codes is not a real flag, just an implementation field, and this test serves to ensure it doesn't affect program behavior.
1418+
-- see also: check-flags.test for the ones that can be check without the full cmdline.test treatment.
1419+
[file a.py]
1420+
#type: ignore
1421+
[out]
1422+
usage: mypy [-h] [-v] [-V] [more options; see below]
1423+
[-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
1424+
mypy: error: unrecognized arguments: --enabled-error-codes=unused-ignore
1425+
== Return code: 2
13911426
[case testImportlibImportCannotBeResolved]
13921427
# cmd: mypy a.py
13931428
# This test is here because it needs use_builtins_fixtures off.

0 commit comments

Comments
 (0)