Skip to content

New setting "user_builtins_name": Ignore specific "name-defined" errors #18936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,15 @@ section of the command line docs.
Note: the exact list of flags enabled by :confval:`strict` may
change over time.

.. confval:: user_builtins_name

:type: comma-separated list of strings

Disable name-defined error checking for the given values.

Note: This setting should be used *only* if new values have been defined
into the ``builtins`` module.


Configuring error messages
**************************
Expand Down
2 changes: 2 additions & 0 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def split_commas(value: str) -> list[str]:
"exclude": lambda s: [s.strip()],
"packages": try_split,
"modules": try_split,
"user_builtins_name": try_split,
}

# Reuse the ini_config_types and overwrite the diff
Expand All @@ -215,6 +216,7 @@ def split_commas(value: str) -> list[str]:
"exclude": str_or_array_as_list,
"packages": try_split,
"modules": try_split,
"user_builtins_name": try_split,
}
)

Expand Down
7 changes: 7 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,13 @@ def add_invertible_flag(
),
group=code_group,
)
code_group.add_argument(
"--user-builtins-name",
action="append",
metavar="NAME",
default=[],
help="List of name to ignore; can repeat for more names",
)
code_group.add_argument(
"-m",
"--module",
Expand Down
3 changes: 3 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class BuildType:
"strict_concatenate",
"strict_equality",
"strict_optional",
"user_builtins_name",
"warn_no_return",
"warn_return_any",
"warn_unreachable",
Expand Down Expand Up @@ -138,6 +139,8 @@ def __init__(self) -> None:
# File names, directory names or subpaths to avoid checking
self.exclude: list[str] = []
self.exclude_gitignore: bool = False
# User defined builtins names to skip name-defined checking
self.user_builtins_name: list[str] = []

# disallow_any options
self.disallow_any_generics = False
Expand Down
3 changes: 3 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6340,6 +6340,9 @@ def lookup(
return None
node = table[name]
return node
# 6. User Defined Builtins
if name in self.options.user_builtins_name:
return None
# Give up.
if not implicit_name and not suppress_errors:
self.name_not_defined(name, ctx)
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -1507,3 +1507,16 @@ def bad_kwargs(**kwargs: Unpack[TVariadic]): # E: Unpack item in ** argument mu
pass

[builtins fixtures/dict.pyi]

[case testUserBuiltinsName]
# flags: --user-builtins-name foo
foo()
egg()
[out]
main:3: error: Name "egg" is not defined

[case testUserBuiltinsNameMultiple]
# flags: --user-builtins-name foo --user-builtins-name egg
foo()
egg()
[out]