Skip to content
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

Black fails to parse match-case statement in black/scripts/generate_schema.py #4233

Open
dankrzeminski32 opened this issue Feb 14, 2024 · 2 comments
Labels
T: bug Something isn't working

Comments

@dankrzeminski32
Copy link
Contributor

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
@dankrzeminski32 dankrzeminski32 added the T: bug Something isn't working label Feb 14, 2024
@hauntsaninja
Copy link
Collaborator

Yeah, I noticed this the other day. I think it's just a target version issue (see also #3294 )

@dankrzeminski32
Copy link
Contributor Author

Thanks, that makes sense. It worked when i used:

black scripts/generate_schema.py -t py310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants