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

Allow for reading TOML files from stdin. #239

Merged

Conversation

fniessink
Copy link
Contributor

When the input filename is '-', read the toml from stdin and output to stdout.

Closes #238.

@fniessink fniessink force-pushed the 238-allow-for-reading-from-stdin branch from fcea87e to 44e1ece Compare July 24, 2024 18:51
.gitignore Outdated Show resolved Hide resolved
src/pyproject_fmt/cli.py Outdated Show resolved Hide resolved
@fniessink fniessink force-pushed the 238-allow-for-reading-from-stdin branch from 586b5ab to 8eb6d9c Compare July 24, 2024 19:18
@fniessink fniessink force-pushed the 238-allow-for-reading-from-stdin branch from ea09cc5 to 8b68b82 Compare July 25, 2024 08:08
@@ -65,6 +66,15 @@ def test_cli_inputs_ok(tmp_path: Path) -> None:
assert len(result) == 3


def test_cli_pyproject_toml_stdin(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("sys.stdin", io.StringIO(""))
Copy link
Member

@gaborbernat gaborbernat Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pytest-mock instead like the rest of the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@fniessink fniessink force-pushed the 238-allow-for-reading-from-stdin branch from 8b68b82 to 7a28ba3 Compare July 30, 2024 15:40
gaborbernat
gaborbernat previously approved these changes Jul 30, 2024
Copy link
Member

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@fniessink fniessink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you like me to deal with the typing error in __main__.py?

In _handle_one, after the if config.stdout block, config.pyproject_toml is never None, but the type checker can't detect that:

def _handle_one(config: Config) -> bool:
    formatted = format_toml(config.toml, config.settings)
    before = config.toml
    changed = before != formatted
    if config.stdout:  # stdout just prints new format to stdout
        print(formatted, end="")  # noqa: T201
        return changed

    if before != formatted and not config.check:
        config.pyproject_toml.write_text(formatted, encoding="utf-8")

I see a few options:

  • casting to Path
  • changing the if config.stdout to if config.pyproject_toml is None or config.stdout

There are probably other options as well.

@gaborbernat
Copy link
Member

either is fine

Copy link
Member

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaborbernat gaborbernat enabled auto-merge (squash) July 30, 2024 16:23
When the input filename is '-', read the toml from stdin and output to stdout.

Closes #238.
auto-merge was automatically disabled July 30, 2024 16:35

Head branch was pushed to by a user without write access

@fniessink fniessink force-pushed the 238-allow-for-reading-from-stdin branch from c14a149 to 5248682 Compare July 30, 2024 16:35
@fniessink
Copy link
Contributor Author

Now with a signed commit.

Copy link
Member

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaborbernat gaborbernat enabled auto-merge (squash) July 30, 2024 16:49
@gaborbernat gaborbernat merged commit 125f7ee into tox-dev:main Jul 30, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for reading from stdin
2 participants