diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 94427a347779..0e033471d2e9 100644 --- a/mypy/config_parser.py +++ b/mypy/config_parser.py @@ -55,8 +55,10 @@ def parse_version(v: str | float) -> tuple[int, int]: def try_split(v: str | Sequence[str], split_regex: str = "[,]") -> list[str]: """Split and trim a str or list of str into a list of str""" if isinstance(v, str): - return [p.strip() for p in re.split(split_regex, v)] - + items = [p.strip() for p in re.split(split_regex, v)] + if items and items[-1] == "": + items.pop(-1) + return items return [p.strip() for p in v] diff --git a/test-data/unit/check-custom-plugin.test b/test-data/unit/check-custom-plugin.test index db2ea2d5e659..feb135bee165 100644 --- a/test-data/unit/check-custom-plugin.test +++ b/test-data/unit/check-custom-plugin.test @@ -1098,3 +1098,15 @@ reveal_type(1) # N: Revealed type is "Literal[1]?" [file mypy.ini] \[mypy] plugins=/test-data/unit/plugins/custom_errorcode.py + + +[case testPyprojectPluginsTrailingComma] +# flags: --config-file tmp/pyproject.toml +[file pyproject.toml] +# This test checks that trailing commas in string-based `plugins` are allowed. +\[tool.mypy] +plugins = """ + /test-data/unit/plugins/function_sig_hook.py, + /test-data/unit/plugins/method_in_decorator.py, +""" +[out] diff --git a/test-data/unit/cmdline.pyproject.test b/test-data/unit/cmdline.pyproject.test index 57e6facad032..f9691ba245f9 100644 --- a/test-data/unit/cmdline.pyproject.test +++ b/test-data/unit/cmdline.pyproject.test @@ -133,3 +133,96 @@ Neither is this! description = "Factory ⸻ A code generator 🏭" \[tool.mypy] [file x.py] + +[case testPyprojectFilesTrailingComma] +# cmd: mypy +[file pyproject.toml] +\[tool.mypy] +# We combine multiple tests in a single one here, because these tests are slow. +files = """ + a.py, + b.py, +""" +always_true = """ + FLAG_A1, + FLAG_B1, +""" +always_false = """ + FLAG_A2, + FLAG_B2, +""" +[file a.py] +x: str = 'x' # ok' + +# --always-true +FLAG_A1 = False +FLAG_B1 = False +if not FLAG_A1: # unreachable + x: int = 'x' +if not FLAG_B1: # unreachable + y: int = 'y' + +# --always-false +FLAG_A2 = True +FLAG_B2 = True +if FLAG_A2: # unreachable + x: int = 'x' +if FLAG_B2: # unreachable + y: int = 'y' +[file b.py] +y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") +[file c.py] +# This should not trigger any errors, because it is not included: +z: int = 'z' +[out] + +[case testPyprojectModulesTrailingComma] +# cmd: mypy +[file pyproject.toml] +\[tool.mypy] +# We combine multiple tests in a single one here, because these tests are slow. +modules = """ + a, + b, +""" +disable_error_code = """ + operator, + import, +""" +enable_error_code = """ + redundant-expr, + ignore-without-code, +""" +[file a.py] +x: str = 'x' # ok + +# --enable-error-code +a: int = 'a' # type: ignore + +# --disable-error-code +'a' + 1 +[file b.py] +y: int = 'y' +[file c.py] +# This should not trigger any errors, because it is not included: +z: int = 'z' +[out] +b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int") +a.py:4: error: "type: ignore" comment without error code (consider "type: ignore[assignment]" instead) + +[case testPyprojectPackagesTrailingComma] +# cmd: mypy +[file pyproject.toml] +\[tool.mypy] +packages = """ + a, + b, +""" +[file a/__init__.py] +x: str = 'x' # ok +[file b/__init__.py] +y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") +[file c/__init__.py] +# This should not trigger any errors, because it is not included: +z: int = 'z' +[out] diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index f298f6dbe2df..b9da5883c793 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -1342,6 +1342,38 @@ always_true = MY_VAR, [out] +[case testCmdlineCfgModulesTrailingComma] +# cmd: mypy +[file mypy.ini] +\[mypy] +modules = + a, + b, +[file a.py] +x: str = 'x' # ok +[file b.py] +y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") +[file c.py] +# This should not trigger any errors, because it is not included: +z: int = 'z' +[out] + +[case testCmdlineCfgPackagesTrailingComma] +# cmd: mypy +[file mypy.ini] +\[mypy] +packages = + a, + b, +[file a/__init__.py] +x: str = 'x' # ok +[file b/__init__.py] +y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") +[file c/__init__.py] +# This should not trigger any errors, because it is not included: +z: int = 'z' +[out] + [case testTypeVarTupleUnpackEnabled] # cmd: mypy --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack a.py [file a.py]