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

Reading configuration from pyproject.toml #356

Open
3 tasks
jspaezp opened this issue Sep 14, 2022 · 5 comments · May be fixed by #412
Open
3 tasks

Reading configuration from pyproject.toml #356

jspaezp opened this issue Sep 14, 2022 · 5 comments · May be fixed by #412
Labels
enhancement New feature or request

Comments

@jspaezp
Copy link

jspaezp commented Sep 14, 2022

Context

It has become increasingly adopted the option to store the configuration of many tools inside the pyproject.toml file.

example for black:
https://ichard26-testblackdocs.readthedocs.io/en/refactor_docs/pyproject_toml.html

It would be amazing if mdformat could read it as well. And since mdformat already supports toml format configurations, it would be trivial to implement looking for the configuration in the tool.mdformat section of one's pyproject.toml file

# .mdformat.toml
#
# This file shows the default values and is equivalent to having
# no configuration file at all. Change the values for non-default
# behavior.
#
wrap = "keep"       # possible values: {"keep", "no", INTEGER}
number = false      # possible values: {false, true}
end_of_line = "lf"  # possible values: {"lf", "crlf", "keep"}
# pyproject.toml

#.......
[tool.mdformat]
wrap = "keep"       # possible values: {"keep", "no", INTEGER}
number = false      # possible values: {false, true}
end_of_line = "lf"  # possible values: {"lf", "crlf", "keep"}

Proposal

I think an approach would be ...

modify this function:

@functools.lru_cache()
def read_toml_opts(conf_dir: Path) -> Mapping:
conf_path = conf_dir / ".mdformat.toml"
if not conf_path.is_file():
parent_dir = conf_dir.parent
if conf_dir == parent_dir:
return {}
return read_toml_opts(parent_dir)
with open(conf_path, "rb") as f:
try:
toml_opts = tomllib.load(f)
except tomllib.TOMLDecodeError as e:
raise InvalidConfError(f"Invalid TOML syntax: {e}")
_validate_keys(toml_opts, conf_path)
_validate_values(toml_opts, conf_path)
return toml_opts

so that:

  • it first looks for the .mdformat.toml
    • if found use it
  • if it is not found, look for pyproject.toml
    • if found, look for a tool.mdformat section
      • if found use that as if it was the contents of .mdformat.toml

LMK what you think, I could make it a PR later in the week

Tasks and updates

  • implement changes
  • add unit testing
  • add documentation
@jspaezp jspaezp added the enhancement New feature or request label Sep 14, 2022
@welcome
Copy link

welcome bot commented Sep 14, 2022

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! 🎉

@hukkin
Copy link
Member

hukkin commented Sep 14, 2022

Thanks for the issue!

I'm inclined to reject this to reduce configuration parsing complexity and to not set a bad precedent for what will be merged in the future.

pyproject.toml is a Python specific file so I really like that Python specific tools (pytest, black, tox, mypy etc.) support it. Mdformat is a programming language agnostic tool, however, so I don't think it should support a Python specific file.

@jspaezp
Copy link
Author

jspaezp commented Sep 15, 2022

Thanks for replying!

I think we can leave this open and see if it gets some traction. I can definitely understand not wanting to add more stuff due to the "maintainance tax" down the road and I understand the rationale behind it.

Implementation right now seems easy so if you need a PR just mention me and I could give it a go.
Best!

@csala
Copy link

csala commented Dec 30, 2022

Definitely in favor of adding support for .pyproject.toml, but @hukkin has a point in this comment about mdformat not being strictly focused on Python projects.

What about adding a --config-file option instead? Then mdformat is not really adhering to any standard in particular other than the toml format, and users are still able to add their settings in the pyproject.toml if it exists, but also in any other configuration file that is parseable as toml (like setup.cfg or any other .ini file).

@csala
Copy link

csala commented Jan 6, 2023

I created this plugin which covers the request of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants