Skip to content

PythonUvFormatter

Base classes

Name Children Inherits
LanguageFormatter
anyenv.language_formatters._base
Abstract base class for language-specific formatters.

⋔ Inheritance diagram

graph TD
  2335156587["_python.PythonUvFormatter"]
  3820931366["_base.LanguageFormatter"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3820931366 --> 2335156587
  925645236 --> 3820931366
  2751680898 --> 925645236

🛈 DocStrings

Bases: LanguageFormatter

Python formatter using uv to run ruff.

Source code in src/anyenv/language_formatters/_python.py
class PythonUvFormatter(LanguageFormatter):
    """Python formatter using uv to run ruff."""

    @property
    def name(self) -> str:
        return "Python"

    @property
    def command(self) -> str:
        return "uv"

    @property
    def extensions(self) -> list[str]:
        return [".py", ".pyi"]

    @property
    def pygments_lexers(self) -> list[str]:
        return ["python", "python3", "py"]

    async def format(self, path: Path) -> FormatResult:
        cmd = ["uv", "run", "ruff", "format", str(path)]
        success, stdout, stderr, duration, error_type = await self._execute_command(cmd)
        return FormatResult(
            success=success,
            output=stdout,
            errors=stderr,
            formatted=success,
            duration=duration,
            error_type=error_type,
        )

    async def lint(self, path: Path, fix: bool = False) -> LintResult:
        cmd = ["uv", "run", "ruff", "check"]
        if fix:
            cmd.extend(["--fix", "--unsafe-fixes"])
        cmd.append(str(path))
        success, stdout, stderr, duration, error_type = await self._execute_command(cmd)
        return LintResult(
            success=success,
            output=stdout,
            errors=stderr,
            duration=duration,
            error_type=error_type,
        )

:fa-brands-github: Show source on GitHub