From 96a27885b2adb30d0a84b01a3acf1ea3b163716e Mon Sep 17 00:00:00 2001 From: un-pogaz <46523284+un-pogaz@users.noreply.github.com> Date: Thu, 17 Apr 2025 16:29:16 +0200 Subject: [PATCH 1/2] New setting "user_builtins_name": Ignore specific "name-defined" errors (#18932) --- docs/source/config_file.rst | 9 +++++++++ mypy/config_parser.py | 2 ++ mypy/main.py | 7 +++++++ mypy/options.py | 3 +++ mypy/semanal.py | 3 +++ test-data/unit/semanal-errors.test | 13 +++++++++++++ 6 files changed, 37 insertions(+) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index de51f0c796fd..673b405c0337 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -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 beed defined + into the ``builtins`` module. + Configuring error messages ************************** diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 0e033471d2e9..8a690765f9db 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -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 @@ -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, } ) diff --git a/mypy/main.py b/mypy/main.py index e5afb05e873b..67de95a072c0 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -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", diff --git a/mypy/options.py b/mypy/options.py index 17fea6b0bf29..98d76c426a21 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -55,6 +55,7 @@ class BuildType: "strict_concatenate", "strict_equality", "strict_optional", + "user_builtins_name", "warn_no_return", "warn_return_any", "warn_unreachable", @@ -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 diff --git a/mypy/semanal.py b/mypy/semanal.py index 586094b7a6fe..5a70ba258fab 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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) diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test index 52c658c97c3b..73b1ad4ae65d 100644 --- a/test-data/unit/semanal-errors.test +++ b/test-data/unit/semanal-errors.test @@ -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] From 0bbcd3aebda74f22ed49febad37fa03ed5deac55 Mon Sep 17 00:00:00 2001 From: un-pogaz <46523284+un-pogaz@users.noreply.github.com> Date: Thu, 17 Apr 2025 19:49:58 +0200 Subject: [PATCH 2/2] fix typo --- docs/source/config_file.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 673b405c0337..b0c580af639b 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -823,7 +823,7 @@ section of the command line docs. Disable name-defined error checking for the given values. - Note: This setting should be used *only* if new values have beed defined + Note: This setting should be used *only* if new values have been defined into the ``builtins`` module.