Skip to content

Commit b476b07

Browse files
authored
Merge pull request #16 from csala/gh-4-Some-options-in-pyproject.toml-are-ignored
Monkey patch to replace read_toml_opts entirely. Resolves #4
2 parents 9f1d852 + 0e96e78 commit b476b07

File tree

4 files changed

+96
-167
lines changed

4 files changed

+96
-167
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Set up Python
1818
uses: actions/setup-python@v4
1919
with:
20-
python-version: 3.11
20+
python-version: 3.13
2121
- name: Installation (deps and package)
2222
run: pip install .
2323
- uses: pre-commit/[email protected]
@@ -49,7 +49,7 @@ jobs:
4949
pytest --cov=mdformat_pyproject --cov-report=xml --cov-report=term-missing
5050
5151
- name: Store PR number and commit SHA
52-
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
52+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
5353
run: |
5454
echo "Storing PR number ${{ github.event.number }}"
5555
echo "${{ github.event.number }}" > pr_number.txt
@@ -63,14 +63,14 @@ jobs:
6363
# Triggered sub-workflow is not able to detect the original commit/PR which is available
6464
# in this workflow.
6565
- name: Store PR number
66-
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
66+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
6767
uses: actions/upload-artifact@v4
6868
with:
6969
name: pr_number
7070
path: pr_number.txt
7171

7272
- name: Store commit SHA
73-
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
73+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
7474
uses: actions/upload-artifact@v4
7575
with:
7676
name: commit_sha
@@ -80,7 +80,7 @@ jobs:
8080
# is executed by a different workflow `coverage-report.yml`. The reason for this
8181
# split is because `on.pull_request` workflows don't have access to secrets.
8282
- name: Store coverage report in artifacts
83-
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
83+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
8484
uses: actions/upload-artifact@v4
8585
with:
8686
name: codecov_report
@@ -89,7 +89,7 @@ jobs:
8989
- run: |
9090
echo "The coverage report was stored in Github artifacts."
9191
echo "It will be uploaded to Codecov using [codecov.yml] workflow shortly."
92-
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.11
92+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.13
9393
9494
pre-commit-hook:
9595
runs-on: ubuntu-latest
@@ -99,7 +99,7 @@ jobs:
9999
- name: Set up Python
100100
uses: actions/setup-python@v4
101101
with:
102-
python-version: 3.11
102+
python-version: 3.13
103103

104104
- name: Installation (deps and package)
105105
run: |
@@ -118,10 +118,10 @@ jobs:
118118
steps:
119119
- name: Checkout source
120120
uses: actions/checkout@v3
121-
- name: Set up Python 3.11
121+
- name: Set up Python 3.13
122122
uses: actions/setup-python@v4
123123
with:
124-
python-version: 3.11
124+
python-version: 3.13
125125
- name: install flit
126126
run: |
127127
pip install flit~=3.0

mdformat_pyproject/plugin.py

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pathlib
44
import sys
5-
from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Union
5+
from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Tuple, Union
66

77
import markdown_it
88
import mdformat
@@ -27,19 +27,14 @@
2727

2828

2929
@cache
30-
def _find_pyproject_toml_path(search_path: str) -> Optional[pathlib.Path]:
31-
"""Find the pyproject.toml file that corresponds to the search path.
30+
def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Path]:
31+
"""Find the pyproject.toml file that applies to the search path.
3232
3333
The search is done ascending through the folders tree until a pyproject.toml
3434
file is found in the same folder. If the root '/' is reached, None is returned.
35-
36-
The special path "-" used for stdin inputs is replaced with the current working
37-
directory.
3835
"""
39-
if search_path == "-":
40-
search_path = pathlib.Path.cwd()
41-
else:
42-
search_path = pathlib.Path(search_path).resolve()
36+
if search_path.is_file():
37+
search_path = search_path.parent
4338

4439
for parent in (search_path, *search_path.parents):
4540
candidate = parent / "pyproject.toml"
@@ -68,50 +63,26 @@ def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]:
6863

6964

7065
@cache
71-
def _reload_cli_opts() -> _ConfigOptions:
72-
"""Re-parse the sys.argv array to deduce which arguments were used in the CLI.
73-
74-
If unknown arguments are found, we deduce that mdformat is being used as a
75-
python library and therefore no mdformat command line arguments were passed.
66+
def read_toml_opts(conf_dir: pathlib.Path) -> Tuple[MutableMapping, Optional[pathlib.Path]]:
67+
"""Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml.
7668
77-
Notice that the strategy above does not fully close the door to situations
78-
with colliding arguments with different meanings, but the rarity of the
79-
situation and the complexity of a possible solution makes the risk worth taking.
69+
Notice that if `.mdformat.toml` exists it is ignored.
8070
"""
81-
import mdformat._cli
82-
83-
if hasattr(mdformat.plugins, "_PARSER_EXTENSION_DISTS"):
84-
# New API, mdformat>=0.7.19
85-
arg_parser = mdformat._cli.make_arg_parser(
86-
mdformat.plugins._PARSER_EXTENSION_DISTS,
87-
mdformat.plugins._CODEFORMATTER_DISTS,
88-
mdformat.plugins.PARSER_EXTENSIONS,
89-
)
71+
pyproject_path = _find_pyproject_toml_path(conf_dir)
72+
if pyproject_path:
73+
pyproject_opts = _parse_pyproject(pyproject_path)
9074
else:
91-
# Backwards compatibility, mdformat<0.7.19
92-
arg_parser = mdformat._cli.make_arg_parser(
93-
mdformat.plugins.PARSER_EXTENSIONS,
94-
mdformat.plugins.CODEFORMATTERS,
95-
)
75+
pyproject_opts = {}
9676

97-
args, unknown = arg_parser.parse_known_args(sys.argv[1:])
98-
if unknown:
99-
return {}
100-
101-
return {key: value for key, value in vars(args).items() if value is not None}
77+
return pyproject_opts, pyproject_path
10278

10379

10480
def update_mdit(mdit: markdown_it.MarkdownIt) -> None:
105-
"""Read the pyproject.toml file and re-create the mdformat options."""
106-
mdformat_options: _ConfigOptions = mdit.options["mdformat"]
107-
file_path = mdformat_options.get("filename", "-")
108-
pyproject_path = _find_pyproject_toml_path(file_path)
109-
if pyproject_path:
110-
pyproject_opts = _parse_pyproject(pyproject_path)
111-
if pyproject_opts is not None:
112-
cli_opts = _reload_cli_opts()
113-
mdformat_options.update(**pyproject_opts)
114-
mdformat_options.update(**cli_opts)
81+
"""No-op, since this plugin only monkey patches and does not modify mdit."""
82+
pass
11583

11684

11785
RENDERERS: MutableMapping[str, "Render"] = {}
86+
87+
# Monkey patch mdformat._conf to use our own read_toml_opts version
88+
mdformat._conf.read_toml_opts = read_toml_opts

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ profile = "black"
4646
[tool.mdformat]
4747
wrap = 99
4848
number = true
49+
exclude = [".tox/**", ".venv/**"]
4950

5051
[tool.coverage.report]
5152
exclude_lines = [

0 commit comments

Comments
 (0)