Skip to main content

Mend your Python projects with reproducible refactors

Project description

👷‍♀️ PyMender

Perform entire codebase refactors in a way that is reproducible, testable and reviewable. Obeys .gitignore by default.

Usage

pip install pymender==0.2.0

pymender <codemod> <path_to_project>

What codemods are available?

FastAPIAnnotated

Converts FastAPI endpoints to use the preferred Annotated[<type>, Depends(<dependency>)] syntax rather than : <type> = Depends(<dependency>).

Why?

  • Default value of the function parameter is the actual default value.
  • Reuse of the function is now possible.
  • Type-safe usage of the functions, previously 'default' values where not type checked.
  • Multi-purpose e.g. Annotated[str, fastapi.Query(), typer.Argument()] is now possible.
pymender FastAPIAnnotated <folder-to-upgrade>
Before After
@router.get('/example')
def example_function(
value: int,
query: str = Query("foo"),
zar: str = Query(default="bar", alias="z"),
foo: str = Depends(get_foo),
*,
bar: str = Depends(get_bar),
body: str = Body(...),
) -> str:
return 'example'
@router.get('/example')
def example_function(
value: int,
foo: Annotated[str, Depends(get_foo)],
query: Annotated[str, Query()] = "foo",
zar: Annotated[str, Query(alias="z")] = "bar",
*,
bar: Annotated[str, Depends(get_bar)],
body: Annotated[str, Body()],
) -> str:
return 'example'

Developer Guide

# Run a particular codemod
python3 -m pymender <codemod> <path_to_project>
# e.g.
python3 -m pymender FastAPIAnnotated <path_to_project>

# Run the codemod directly
python3 -m libcst.tool codemode fastapi_annotated.FastAPIAnnotated <path_to_project>

# Run tests
pytest -vv

Thanks to:

  • libCST which does a lot of the hardwork for this.
  • autotyping for showing what was possible.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymender-0.2.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

pymender-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pymender-0.2.0.tar.gz.

File metadata

  • Download URL: pymender-0.2.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pymender-0.2.0.tar.gz
Algorithm Hash digest
SHA256 101bd7adf14ea98ede73ef34a76b1a9e2bb77ff59cdc182ba84837a619f617f8
MD5 6f235e0faa67b4d405d65e568ad370cc
BLAKE2b-256 96cd4cce24b78e351d9a4c821324db18c180365b10dd00074589a75f849f15ec

See more details on using hashes here.

File details

Details for the file pymender-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pymender-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for pymender-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e87bd8a84d3d8a54406878edb7776a6c0deb66ed5d581dc1d8e25ee9d361964
MD5 bec4f9635f2321d9e80d94b6b22cbace
BLAKE2b-256 686e81e3aac18379dba845994114c2437b5655b223bbfbf78d1e9dd38e60b6cb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page