Skip to content

Commit

Permalink
Add ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Oct 2, 2023
1 parent bb41c67 commit 4a147e3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 31 deletions.
23 changes: 8 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
exclude: (^tests)
exclude: (^tests|docs|examples)
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
hooks:
- id: ruff
args: [ --fix ]
stages: [ commit ]

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
args: [ --line-length=120, --skip-string-normalization ]
stages: [ commit ]

- repo: https://github.com/myint/autoflake
rev: v1.4
hooks:
- id: autoflake
args: [ --in-place, --ignore-init-module-imports, --remove-all-unused-imports, --remove-unused-variables ]
stages: [ commit ]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: [ --profile=black, --force-grid-wrap=2, --lines-after-imports=2, --force-single-line, --line-length=120, --project=pythogen]
stages: [ commit ]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.961
hooks:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Build Status](https://github.com/artsmolin/pythogen/actions/workflows/main.yml/badge.svg)](https://github.com/artsmolin/pythogen/actions)
[![codecov](https://codecov.io/gh/artsmolin/pythogen/branch/main/graph/badge.svg?token=6JR6NB8Y9Z)](https://codecov.io/gh/artsmolin/pythogen)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
![downloads](https://img.shields.io/pypi/dm/pythogen)
[![python](https://img.shields.io/pypi/pyversions/pythogen.svg)](https://pypi.python.org/pypi/pythogen/)
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Generator of python HTTP-clients from OpenApi specification based on [httpx](htt


[![codecov](https://codecov.io/gh/artsmolin/pythogen/branch/main/graph/badge.svg?token=6JR6NB8Y9Z)](https://codecov.io/gh/artsmolin/pythogen)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
![downloads](https://img.shields.io/pypi/dm/pythogen)
[![python](https://img.shields.io/pypi/pyversions/pythogen.svg)](https://pypi.python.org/pypi/pythogen/)
Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,30 @@ pre-commit = "^2.19.0"
httpx = "^0.24.1"
pydantic = "^2.0.0"
pytest-asyncio = "^0.21.1"
ruff = "^0.0.292"

[build-system]
requires = ["poetry>=1.4.0"]
build-backend = "poetry.masonry.api"

[tool.black]
line-length = 120

[tool.ruff]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
ignore = [
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"C901", # too complex
]

[tool.ruff.isort]
lines-after-imports = 2
force-single-line = true
20 changes: 10 additions & 10 deletions pythogen/parsers/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ def _parse_type(self, data: dict[str, Any]) -> models.Type:
raw_data_type: str | None = data.get('type')
try:
data_type = models.Type(raw_data_type)
except ValueError:
raise Exception(f'Unable to parse schema, unknown type "{raw_data_type}" on "{data}"')
except ValueError as exc:
raise Exception(f'Unable to parse schema, unknown type "{raw_data_type}" on "{data}"') from exc
return data_type

def _parse_format(self, data: dict[str, Any]) -> models.Format | None:
data_format = data.get('format')
if data_format:
try:
return models.Format(data_format)
except Exception:
raise Exception(f'Unable to parse schema, unknown format "{data_format}"')
except Exception as exc:
raise Exception(f'Unable to parse schema, unknown format "{data_format}"') from exc
return None

def _get_description(self, data: dict[str, Any]) -> str | None:
Expand All @@ -214,17 +214,17 @@ def _parse_discriminator(self, data: dict[str, Any]) -> models.Discriminator | N
property_name: str | None = raw_discriminator.get("propertyName")
if not property_name:
console.print_error(
title=f"Failed to generate a client",
msg=f"The discriminator must contain the \"propertyName\" field.",
title="Failed to generate a client",
msg="The discriminator must contain the \"propertyName\" field.",
invalid_data=data,
)
raise exceptions.Exit()

raw_mapping: dict[str, Any] | None = raw_discriminator.get("mapping")
if not raw_mapping:
console.print_error(
title=f"Failed to generate a client",
msg=f"The discriminator must contain the \"mapping\" field.",
title="Failed to generate a client",
msg="The discriminator must contain the \"mapping\" field.",
invalid_data=data,
)
raise exceptions.Exit()
Expand Down Expand Up @@ -264,8 +264,8 @@ def _parse_properties(
if data_format:
try:
data_format = models.Format(data_format)
except Exception:
raise Exception(f'Unable to parse schema "{id}", unknown format "{data_format}"')
except Exception as exc:
raise Exception(f'Unable to parse schema "{id}", unknown format "{data_format}"') from exc

properties = []

Expand Down
10 changes: 5 additions & 5 deletions pythogen/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def iterresponsemap(responses: models.ResponsesObject) -> list[tuple[str, str]]:
mapping.append((code, mapper))
continue

mapper = f'response.json()'
mapper = 'response.json()'
mapping.append((code, mapper))
continue
continue
Expand All @@ -199,7 +199,7 @@ def iterresponsemap(responses: models.ResponsesObject) -> list[tuple[str, str]]:
continue

if response.schema.type == models.Type.integer:
mapping.append((code, f'int(response.text)'))
mapping.append((code, 'int(response.text)'))
continue

raise NotImplementedError(
Expand All @@ -219,7 +219,7 @@ def j2_responserepr(responses: models.ResponsesObject, document: models.Document
else:
types.append(j2_typerepr(response.schema, document))

types = sorted(list(set(types)))
types = sorted(set(types))

if not types:
return 'None'
Expand Down Expand Up @@ -290,14 +290,14 @@ def j2_repr_any_of(any_of_items: list[models.SchemaObject], document: models.Doc


def varname(value: str) -> str:
clean_value = re.sub('\W|^(?=\d)', '_', value) # remove special characters
clean_value = re.sub(r'\W|^(?=\d)', '_', value) # remove special characters
clean_value = re.sub('_{2,}', '_', clean_value) # __ -> _
clean_value = clean_value.replace(' ', '_')
return inflection.underscore(clean_value)


def classname(value: str) -> str:
clean_value = re.sub('\W|^(?=\d)', '_', value) # remove special characters
clean_value = re.sub(r'\W|^(?=\d)', '_', value) # remove special characters
clean_value = re.sub('_{2,}', '_', clean_value) # __ -> _
return inflection.camelize(clean_value)

Expand Down

0 comments on commit 4a147e3

Please sign in to comment.