Black fails to parse match-case statement in `black/scripts/generate_schema.py`
Describe the bug
I found this bug while trying to run black on it's own source code.
The file generate_schema.py in the scripts folder failed to get parsed properly.
To Reproduce
This is the function that causes the issue,
def generate_schema_from_click(
cmd: click.Command,
) -> dict[str, Any]:
result: dict[str, dict[str, Any]] = {}
for param in cmd.params:
if not isinstance(param, click.Option) or param.is_eager:
continue
assert param.name
name = param.name.replace("_", "-")
result[name] = {}
match param.type:
case click.types.IntParamType():
result[name]["type"] = "integer"
case click.types.StringParamType() | click.types.Path():
result[name]["type"] = "string"
case click.types.Choice(choices=choices):
result[name]["enum"] = choices
case click.types.BoolParamType():
result[name]["type"] = "boolean"
case _:
msg = f"{param.type!r} not a known type for {param}"
raise TypeError(msg)
if param.multiple:
result[name] = {"type": "array", "items": result[name]}
result[name]["description"] = param.help
if param.default is not None and not param.multiple:
result[name]["default"] = param.default
return result
it is found in black's source code under scripts/generate_schema.py, you can run black on that file:
$ black scripts/generate_schema.py
The resulting error is:
error: cannot format scripts/generate_schema.py: Cannot parse: 22:14: match param.type:
Oh no! 💥 💔 💥 1 file failed to reformat.
Expected behavior
black parses the file correctly and potentially formats the code.
Environment
- Black's version: 24.2.0 (compiled: yes)
- OS and Python version: macOS, python 3.11.3
Yeah, I noticed this the other day. I think it's just a target version issue (see also #3294 )
Thanks, that makes sense. It worked when i used:
black scripts/generate_schema.py -t py310