Skip to content

Commit

Permalink
Rewrite on Pydantic (#104)
Browse files Browse the repository at this point in the history
* PWSH -> nox

Signed-off-by: GitHub <[email protected]>

* Update lints

Signed-off-by: GitHub <[email protected]>

* Add Pydantic

Signed-off-by: GitHub <[email protected]>

* Raw conversion of dataclasses to models

Signed-off-by: GitHub <[email protected]>

* Get tests passing

Signed-off-by: GitHub <[email protected]>

* Remove extraneous dash

Signed-off-by: GitHub <[email protected]>

* Remove another extraneous dash

Signed-off-by: GitHub <[email protected]>

* Add tests for new Python verisons

Signed-off-by: GitHub <[email protected]>

---------

Signed-off-by: GitHub <[email protected]>
  • Loading branch information
shenanigansd authored Nov 24, 2024
1 parent 70ee6c1 commit 7103c8f
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 398 deletions.
5 changes: 0 additions & 5 deletions .devcontainer/devcontainer-lock.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"features": {
"ghcr.io/devcontainers/features/powershell:1": {
"version": "1.3.5",
"resolved": "ghcr.io/devcontainers/features/powershell@sha256:0dd4e0352cc77ef586f7cca2414d3e8a7c506ad6df9ecd2221d078a961425bd6",
"integrity": "sha256:0dd4e0352cc77ef586f7cca2414d3e8a7c506ad6df9ecd2221d078a961425bd6"
},
"ghcr.io/devcontainers/features/python:1": {
"version": "1.6.1",
"resolved": "ghcr.io/devcontainers/features/python@sha256:d449aea663ea23ac4a7968719d5920dd57128f0429cd8e216849d5afe67651fb",
Expand Down
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"ghcr.io/devcontainers/features/python:1": {
"version": "3.12",
"installTools": false
},
"ghcr.io/devcontainers/features/powershell:1": {}
}
}
}
2 changes: 1 addition & 1 deletion .github/workflows/python-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
python-version: [ "3.11" ]
python-version: [ "3.11", "3.12", "3.13" ]

uses: darbiadev/.github/.github/workflows/python-test.yaml@29197a38ef3741064f47b623ede0c1ad22402c57 # v13.0.3
with:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v5.0.0
hooks:
- id: check-case-conflict
- id: check-merge-conflict
Expand Down
4 changes: 2 additions & 2 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Changelog

- :release:`5.2.1 <14th November 2024>`
- :bug:`100` Allow new fields to be dynamic
-

- :release:`5.2.0 <14th November 2024>`
- :bug:`100` Add fields for fields for PEP 639, Metadata 2.4
-

- :release:`5.1.0 <26th February 2024>`
- :bug:`78` Add ``dynamic`` and ``provides_extra`` to JSON schema

Expand Down
6 changes: 3 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def linkcode_resolve(domain: str, info: dict) -> str:
if not info["module"]:
return None

import importlib # noqa: PLC0415
import inspect # noqa: PLC0415
import types # noqa: PLC0415
import importlib
import inspect
import types

mod = importlib.import_module(info["module"])

Expand Down
119 changes: 0 additions & 119 deletions make.ps1

This file was deleted.

52 changes: 52 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Noxfile."""

import shutil
from pathlib import Path

import nox

nox.options.default_venv_backend = "none"
nox.options.sessions = ["lints"]


CLEANABLE_TARGETS = [
"./dist",
"./build",
"./.nox",
"./.coverage",
"./.coverage.*",
"./coverage.json",
"./**/.mypy_cache",
"./**/.pytest_cache",
"./**/__pycache__",
"./**/*.pyc",
"./**/*.pyo",
]


@nox.session
def tests(session: nox.Session) -> None:
"""Run tests."""
session.run("pytest")


@nox.session
def lints(session: nox.Session) -> None:
"""Run lints."""
session.run("pre-commit", "run", "--all-files")
session.run("ruff", "format", ".")
session.run("ruff", "check", "--fix", ".")
session.run("mypy", "--strict", "src/")


@nox.session
def clean(_: nox.Session) -> None:
"""Clean cache, .pyc, .pyo, and test/build artifact files from project."""
count = 0
for searchpath in CLEANABLE_TARGETS:
for filepath in Path().glob(searchpath):
if filepath.is_dir():
shutil.rmtree(filepath)
else:
filepath.unlink()
count += 1
26 changes: 14 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
name = "letsbuilda-pypi"
version = "5.2.1"
description = "A wrapper for PyPI's API and RSS feed"
authors = [
{ name = "Bradley Reynolds", email = "[email protected]" },
]
authors = [{ name = "Bradley Reynolds", email = "[email protected]" }]
license = { text = "MIT" }
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"httpx",
"xmltodict",
"pydantic",
]

[project.urls]
Expand All @@ -20,6 +19,7 @@ documentation = "https://docs.letsbuilda.dev/letsbuilda-pypi/"
[project.optional-dependencies]
dev = [
"pre-commit",
"nox",
"ruff",
"mypy",
"types-xmltodict",
Expand All @@ -42,26 +42,24 @@ build-backend = "setuptools.build_meta"
"letsbuilda.pypi" = ["py.typed"]

[tool.ruff]
preview = true
unsafe-fixes = true
target-version = "py311"
target-version = "py312"
line-length = 120

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"CPY001", # (Missing copyright notice at top of file)
"CPY001", # (Missing copyright notice at top of file)
"PLC0414", # (Import alias does not rename original package) - Re-exporting
]

[tool.ruff.lint.extend-per-file-ignores]
"docs/*" = [
"INP001", # (File `tests/*.py` is part of an implicit namespace package. Add an `__init__.py`.) - Docs are not modules
"FA102", # (Missing `from __future__ import annotations`, but uses PEP 585 collection) - Docs are actually built on the latest stable release of Python
"FA102", # (Missing `from __future__ import annotations`, but uses PEP 585 collection) - Docs are actually built on the latest stable release of Python
]
"tests/*" = [
"INP001", # (File `tests/*.py` is part of an implicit namespace package. Add an `__init__.py`.) - Tests are not modules
"S101", # (Use of `assert` detected) - Yes, that's the point
"S101", # (Use of `assert` detected) - Yes, that's the point
]

[tool.ruff.lint.isort]
Expand All @@ -70,7 +68,11 @@ known-first-party = ["letsbuilda.pypi"]
[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.mypy]
plugins = ["pydantic.mypy"]

[tool.coverage.run]
source = [
"letsbuilda.pypi",
]
source = ["letsbuilda.pypi"]

[tool.pytest.ini_options]
addopts = "--strict-markers"
8 changes: 5 additions & 3 deletions src/letsbuilda/pypi/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def get_rss_feed(self: Self, feed_url: str) -> list[RSSPackageMetadata]:
"""
response = await self.http_client.get(feed_url)
rss_data = xmltodict.parse(response.text)["rss"]["channel"]["item"]
return [RSSPackageMetadata.build_from(package_data) for package_data in rss_data]
return [RSSPackageMetadata.model_validate(package_data) for package_data in rss_data]

async def get_package_json_metadata(
self: Self,
Expand Down Expand Up @@ -68,7 +68,7 @@ async def get_package_json_metadata(
response = await self.http_client.get(url)
if response.status_code == HTTPStatus.NOT_FOUND:
raise PackageNotFoundError(package_title, package_version)
return JSONPackageMetadata.from_dict(response.json())
return JSONPackageMetadata.model_validate(response.json())

async def get_package_metadata(
self: Self,
Expand All @@ -89,4 +89,6 @@ async def get_package_metadata(
Package
The package object.
"""
return Package.from_json_api_data(await self.get_package_json_metadata(package_title, package_version))
return Package.model_validate(
(await self.get_package_json_metadata(package_title, package_version)).model_dump(),
)
Loading

0 comments on commit 7103c8f

Please sign in to comment.