Skip to content

Commit 96a2788

Browse files
committed
New setting "user_builtins_name": Ignore specific "name-defined" errors (python#18932)
1 parent 454989f commit 96a2788

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

docs/source/config_file.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,15 @@ section of the command line docs.
817817
Note: the exact list of flags enabled by :confval:`strict` may
818818
change over time.
819819

820+
.. confval:: user_builtins_name
821+
822+
:type: comma-separated list of strings
823+
824+
Disable name-defined error checking for the given values.
825+
826+
Note: This setting should be used *only* if new values have beed defined
827+
into the ``builtins`` module.
828+
820829

821830
Configuring error messages
822831
**************************

mypy/config_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def split_commas(value: str) -> list[str]:
193193
"exclude": lambda s: [s.strip()],
194194
"packages": try_split,
195195
"modules": try_split,
196+
"user_builtins_name": try_split,
196197
}
197198

198199
# Reuse the ini_config_types and overwrite the diff
@@ -215,6 +216,7 @@ def split_commas(value: str) -> list[str]:
215216
"exclude": str_or_array_as_list,
216217
"packages": try_split,
217218
"modules": try_split,
219+
"user_builtins_name": try_split,
218220
}
219221
)
220222

mypy/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,13 @@ def add_invertible_flag(
12821282
),
12831283
group=code_group,
12841284
)
1285+
code_group.add_argument(
1286+
"--user-builtins-name",
1287+
action="append",
1288+
metavar="NAME",
1289+
default=[],
1290+
help="List of name to ignore; can repeat for more names",
1291+
)
12851292
code_group.add_argument(
12861293
"-m",
12871294
"--module",

mypy/options.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class BuildType:
5555
"strict_concatenate",
5656
"strict_equality",
5757
"strict_optional",
58+
"user_builtins_name",
5859
"warn_no_return",
5960
"warn_return_any",
6061
"warn_unreachable",
@@ -138,6 +139,8 @@ def __init__(self) -> None:
138139
# File names, directory names or subpaths to avoid checking
139140
self.exclude: list[str] = []
140141
self.exclude_gitignore: bool = False
142+
# User defined builtins names to skip name-defined checking
143+
self.user_builtins_name: list[str] = []
141144

142145
# disallow_any options
143146
self.disallow_any_generics = False

mypy/semanal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6340,6 +6340,9 @@ def lookup(
63406340
return None
63416341
node = table[name]
63426342
return node
6343+
# 6. User Defined Builtins
6344+
if name in self.options.user_builtins_name:
6345+
return None
63436346
# Give up.
63446347
if not implicit_name and not suppress_errors:
63456348
self.name_not_defined(name, ctx)

test-data/unit/semanal-errors.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,3 +1507,16 @@ def bad_kwargs(**kwargs: Unpack[TVariadic]): # E: Unpack item in ** argument mu
15071507
pass
15081508

15091509
[builtins fixtures/dict.pyi]
1510+
1511+
[case testUserBuiltinsName]
1512+
# flags: --user-builtins-name foo
1513+
foo()
1514+
egg()
1515+
[out]
1516+
main:3: error: Name "egg" is not defined
1517+
1518+
[case testUserBuiltinsNameMultiple]
1519+
# flags: --user-builtins-name foo --user-builtins-name egg
1520+
foo()
1521+
egg()
1522+
[out]

0 commit comments

Comments
 (0)