Skip to content
New issue

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

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

Already on GitHub? # to your account

pyproject.toml detection fails in subdirectories when the file has no [tool.black] section since black 24.2 #2052

Open
meliache opened this issue Aug 2, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@meliache
Copy link
Contributor

meliache commented Aug 2, 2024

Describe the bug
Since black release v24.2 black ignores pyproject.toml files without a [tool.black] section. The bug arises because datamodel-codegen uses black's black.find_project_root here, but that now that ignores pyproject.toml files without a [tool.black] section, even if there is a [tool.datamodel-codegen] section, as defined here:

    for directory in (common_base, *common_base.parents):
        if (directory / ".git").exists():
            return directory, ".git directory"

        if (directory / ".hg").is_dir():
            return directory, ".hg directory"

        if (directory / "pyproject.toml").is_file():
            pyproject_toml = _load_toml(directory / "pyproject.toml")
            if "black" in pyproject_toml.get("tool", {}):
                return directory, "pyproject.toml"

At my job I'm working in a monorepo where the python package and the pyproject.toml are in a subdirectory, i.e. with the structure <git root>/<python root>/pyproject.toml>. We use ruff to reformat all generated files, so we didn't have a black config in the pyproject.toml. I noticed that datamodel-codegen could not fild our pyproject.toml. It assumed the project root is the git root, since the pyproject.toml was ignored (see code snippet above).

To Reproduce

  • create a git repo where the pyproject.toml is in a subdirectory of the git root and has some [tool.datamodel-codegen] configuration, but no [tool.black] section
  • use black 24.2.2 and datamodel-code-generator 0.25.8
  • run datamodel-codegen from within the subdirectory with the pyproject.toml on some specification and see if the tool recognizes the config options

Expected behavior
Recognize pyproject.toml if it's in subdirectory and has no [tool.black] section, as long as it has [tool.datamodel-codegen] section.

Version:

  • OS: macOS 14.5
  • Python version: 3.11.9
  • datamodel-code-generator version: 0.25.8
meliache pushed a commit to meliache/black that referenced this issue Aug 2, 2024
Extend the docstring of black's `find_project_root` to mention that it ignores
`pyproject.toml` files without a `[tool.black]` section.

This is relevant because that function is also used by other python packages
that use black. I found that e.g. datamodel-code-generator [1] uses that
function and that there the ignoring of the pyproject.toml files lead to
a degradation [2]. I think in that case it would be better to not use black's function
for finding the pyproject.toml, but in any case this behavior should be documented.

1: https://github.com/koxudaxi/datamodel-code-generator
2: koxudaxi/datamodel-code-generator#2052
meliache pushed a commit to meliache/black that referenced this issue Aug 2, 2024
Extend the docstring of black's `find_project_root` to mention that it ignores
`pyproject.toml` files without a `[tool.black]` section.

This is relevant because that function is also used by other python packages
that use black. I found that e.g. datamodel-code-generator [1] uses that
function and that there the ignoring of the pyproject.toml files lead to
a degradation [2]. I think in that case it would be better to not use black's function
for finding the pyproject.toml, but in any case this behavior should be documented.

1: https://github.com/koxudaxi/datamodel-code-generator
2: koxudaxi/datamodel-code-generator#2052
meliache pushed a commit to meliache/black that referenced this issue Aug 2, 2024
Extend the docstring of black's `find_project_root` to mention that it ignores
`pyproject.toml` files without a `[tool.black]` section.

This is relevant because that function is also used by other python packages
that use black. I found that e.g. datamodel-code-generator [1] uses that
function and that there the ignoring of the pyproject.toml files lead to
a degradation [2]. I think in that case it would be better to not use black's function
for finding the pyproject.toml, but in any case this behavior should be documented.

1: https://github.com/koxudaxi/datamodel-code-generator
2: koxudaxi/datamodel-code-generator#2052
meliache pushed a commit to meliache/black that referenced this issue Aug 2, 2024
Extend the docstring of black's `find_project_root` to mention that it ignores
`pyproject.toml` files without a `[tool.black]` section.

This is relevant because that function is also used by other python packages
that use black. I found that e.g. datamodel-code-generator [1] uses that
function and that there the ignoring of the pyproject.toml files lead to
a degradation [2]. I think in that case it would be better to not use black's function
for finding the pyproject.toml, but in any case this behavior should be documented.

1: https://github.com/koxudaxi/datamodel-code-generator
2: koxudaxi/datamodel-code-generator#2052
JelleZijlstra pushed a commit to psf/black that referenced this issue Aug 2, 2024
….black]` (#4425)


Extend the docstring of black's `find_project_root` to mention that it ignores
`pyproject.toml` files without a `[tool.black]` section.

This is relevant because that function is also used by other python packages
that use black. I found that e.g. datamodel-code-generator [1] uses that
function and that there the ignoring of the pyproject.toml files lead to
a degradation [2]. I think in that case it would be better to not use black's function
for finding the pyproject.toml, but in any case this behavior should be documented.

1: https://github.com/koxudaxi/datamodel-code-generator
2: koxudaxi/datamodel-code-generator#2052

Co-authored-by: Michael Eliachevitch <Michael.Eliachevitch@blueyonder.com>
@koxudaxi koxudaxi added the bug Something isn't working label Nov 24, 2024
@otonnesen
Copy link

My work also ran into this issue recently. I just opened #2242 to address it.

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

No branches or pull requests

3 participants