-
Notifications
You must be signed in to change notification settings - Fork 1
feat: pydantic #12
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
Merged
Merged
feat: pydantic #12
Changes from all commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
5b6bd80
chore(deps): add pydantic and pyld to dependency
AmaseCocoa 9351edf
feat: migrate to pydantic
AmaseCocoa 3d953e4
Merge branch 'develop' into next
AmaseCocoa 95eb3bb
feat: use pydantic-based dumper
AmaseCocoa c89ae6d
Merge branch 'next' of https://github.com/fedi-libs/apmodel into next
AmaseCocoa 9ea90d7
chore: remove unused codes
AmaseCocoa 325a3ad
feat: migrate to pydantic
AmaseCocoa 48ac57d
fix: pydantic
AmaseCocoa ca05723
fix: allow extra value
AmaseCocoa 4f26053
fix: rename custom_to_json to _inference_context for avoid recursion
AmaseCocoa 344769a
fix: add serialize_by_alias
AmaseCocoa 32b11e1
feat: add to_dict function
AmaseCocoa 0a3ce22
fix(test): remove debug code
AmaseCocoa 004794c
fix: use Unpack from typing_extensions
AmaseCocoa 3a3c264
chore(deps): add typing-extensions
AmaseCocoa dc6c385
chore(ci): add 3.14 to test
AmaseCocoa a83c1f9
fix(ci): use uv v9
AmaseCocoa 137d651
chore: add test
AmaseCocoa cd27f80
docs: create documents
AmaseCocoa c3c1abb
chore(ci): bump uv version
AmaseCocoa aadbf4a
feat: add extra models from litepub (EmojiReact)
AmaseCocoa fb3906c
fix: add _model_type to all models
AmaseCocoa 8cdca6c
feat: preload apmodel's models for better performance
AmaseCocoa c145b1b
fix: fix circler import
AmaseCocoa 42bad9f
perf: implement lazy loading for model registry
AmaseCocoa 5535dd0
docs(registry): add documentation for model registry
AmaseCocoa 44677af
chore: add task docs:dev
AmaseCocoa b0e294c
refactor(registry): accept preload_mapping in ModelRegistry constructor
AmaseCocoa 8a8f3f9
test(registry): add comprehensive tests for ModelRegistry
AmaseCocoa eb251bb
fix: add core models to bootstrap type mapping
AmaseCocoa 26e0aab
feat: include litepub-0.1.jsonld to preloaded schema
AmaseCocoa 9715409
fix: fix type annotation
AmaseCocoa b3c0a34
fix: support litepub 0.1 schema
AmaseCocoa 1a3dfbf
feat(tests): add Akkoma actor, note, and replies test data
AmaseCocoa 88154a4
feat(core): implement likes and shares in Object model
AmaseCocoa a54f57f
chore: cleanup test code
AmaseCocoa 1613af4
feat(tests): add test data from mastodon 4.x
AmaseCocoa 0f056d0
feat(tests): setup codecov
AmaseCocoa 235f040
chore: cleanup unused codes
AmaseCocoa 206e6c5
fix(ci): update pytest coverage command
AmaseCocoa 7dcdb54
fix: add TypeAlias to Literal
AmaseCocoa 1f27920
docs(readme): Update README with detailed project info and usage
AmaseCocoa 498c0ae
docs(readme): add pre-commit.ci status
AmaseCocoa 75ff333
refactor(lint): fix lint error
AmaseCocoa 551d317
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8bd154a
fix(tests): remove skip and fix nodeinfo test
AmaseCocoa 39e5f9b
feat(dev): setup pyrefly
AmaseCocoa 1dd0bf1
Merge branch 'next' of https://github.com/fedi-libs/apmodel into next
AmaseCocoa f5e9308
fix(agents): fix to correct directory
AmaseCocoa fd1adaf
fix(core): merge field_validator into one function
AmaseCocoa 8647242
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dc18468
feat(pre-commit): add pyrefly to pre-commit config
AmaseCocoa 9306165
fix(type): fix types
AmaseCocoa bfc8b28
fix(type): Resolve all pyrefly type errors
AmaseCocoa 4fe5bf1
Merge branch 'next' of https://github.com/fedi-libs/apmodel into next
AmaseCocoa c397a2b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6b51800
fix(pre-commit): use managed pyrefly install
AmaseCocoa 7b5a348
Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks"
AmaseCocoa 6515fd1
Merge branch 'next' of https://github.com/fedi-libs/apmodel into next
AmaseCocoa 6844d37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1cefea4
fix(collection): Properly load OrderedCollectionPage and handle ordered
AmaseCocoa cb209d6
Merge branch 'next' of https://github.com/fedi-libs/apmodel into next
AmaseCocoa 32498ac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6251d07
fix(pre-commit): add dev-dependencies to additional_dependencies
AmaseCocoa 293fe35
Merge branch 'next' of https://github.com/fedi-libs/apmodel into next
AmaseCocoa 05d9c53
feat(pre-commit): automate sync dependencies from pyproject.toml
AmaseCocoa c6e75ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1c30a38
fix(multibase): use correct format
AmaseCocoa bbfab5b
fix(multibase): use PKCS8
AmaseCocoa cb1ede4
feat(object): separate _inference_context
AmaseCocoa bbaa4f9
fix(jsonld): always serialize @context as a list
AmaseCocoa 64de373
feat(models): add ActorEndpoints and expand model test coverage
AmaseCocoa 0b1c493
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Documentation | ||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| - main | ||
| permissions: | ||
| contents: read | ||
| pages: write | ||
| id-token: write | ||
| jobs: | ||
| deploy: | ||
| environment: | ||
| name: github-pages | ||
| url: ${{ steps.deployment.outputs.page_url }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/configure-pages@v5 | ||
| - uses: actions/checkout@v5 | ||
| - name: Setup uv | ||
| uses: astral-sh/setup-uv@v6 | ||
| - run: | | ||
| uv sync --dev | ||
| zensical build --clean | ||
| - uses: actions/upload-pages-artifact@v4 | ||
| with: | ||
| path: site | ||
| - uses: actions/deploy-pages@v4 | ||
| id: deployment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| repos: | ||
| - hooks: | ||
| - additional_dependencies: | ||
| - cryptography>=45.0.6 | ||
| - git-cliff>=2.10.0 | ||
| - mkdocstrings-python>=2.0.1 | ||
| - multiformats>=0.3.1.post4 | ||
| - pydantic>=2.12.5 | ||
| - pyld[aiohttp,requests]>=2.0.4 | ||
| - pyrefly>=0.46.0 | ||
| - pytest-cov>=7.0.0 | ||
| - pytest>=8.4.1 | ||
| - pyyaml>=6.0.3 | ||
| - typing-extensions>=4.15.0 | ||
| - zensical>=0.0.13 | ||
| args: | ||
| - --fix | ||
| description: Run 'ruff' for extremely fast Python linting | ||
| entry: ruff check --force-exclude | ||
| id: ruff | ||
| language: python | ||
| minimum_pre_commit_version: 2.9.2 | ||
| name: ruff | ||
| require_serial: true | ||
| types_or: | ||
| - python | ||
| - pyi | ||
| - additional_dependencies: | ||
| - cryptography>=45.0.6 | ||
| - git-cliff>=2.10.0 | ||
| - mkdocstrings-python>=2.0.1 | ||
| - multiformats>=0.3.1.post4 | ||
| - pydantic>=2.12.5 | ||
| - pyld[aiohttp,requests]>=2.0.4 | ||
| - pyrefly>=0.46.0 | ||
| - pytest-cov>=7.0.0 | ||
| - pytest>=8.4.1 | ||
| - pyyaml>=6.0.3 | ||
| - typing-extensions>=4.15.0 | ||
| - zensical>=0.0.13 | ||
| args: [] | ||
| description: Run 'ruff format' for extremely fast Python formatting | ||
| entry: ruff format --force-exclude | ||
| id: ruff-format | ||
| language: python | ||
| minimum_pre_commit_version: 2.9.2 | ||
| name: ruff-format | ||
| require_serial: true | ||
| types_or: | ||
| - python | ||
| - pyi | ||
| repo: https://github.com/astral-sh/ruff-pre-commit | ||
| rev: v0.1.6 | ||
| - hooks: | ||
| - additional_dependencies: | ||
| - cryptography>=45.0.6 | ||
| - git-cliff>=2.10.0 | ||
| - mkdocstrings-python>=2.0.1 | ||
| - multiformats>=0.3.1.post4 | ||
| - pydantic>=2.12.5 | ||
| - pyld[aiohttp,requests]>=2.0.4 | ||
| - pyrefly>=0.46.0 | ||
| - pytest-cov>=7.0.0 | ||
| - pytest>=8.4.1 | ||
| - pyyaml>=6.0.3 | ||
| - typing-extensions>=4.15.0 | ||
| - zensical>=0.0.13 | ||
| id: pyrefly-check | ||
| name: Pyrefly (type checking) | ||
| pass_filenames: false | ||
| repo: https://github.com/facebook/pyrefly-pre-commit | ||
| rev: 0.46.0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "languages": { | ||
| "Python": { | ||
| "language_servers": ["ruff", "pyrefly"] | ||
| } | ||
| }, | ||
| "lsp": { | ||
| "pyrefly": { | ||
| "binary": { | ||
| "path": ".venv/bin/pyrefly", | ||
| "arguments": ["lsp"] | ||
| }, | ||
| "settings": { | ||
| "python": { | ||
| "pythonPath": ".venv/bin/python" | ||
| }, | ||
| "pyrefly": { | ||
| "project_includes": ["src/**/*.py", "tests/**/*.py"], | ||
| "project_excludes": ["**/.[!/.]*", "**/*venv/**"], | ||
| "search_path": ["src"], | ||
| "ignore_errors_in_generated_code": true | ||
| } | ||
| } | ||
| }, | ||
| "ruff": { | ||
| "initialization_options": { | ||
| "settings": { | ||
| "lineLength": 80, | ||
| "lint": { | ||
| "extendSelect": ["I"] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,105 @@ | ||
| # apmodel | ||
| ActivityStreams 2.0 and nodeinfo 2.0/2.1 implementation for Python. | ||
| ## Other ActivityPub Tools | ||
| - [apsig: Signature implementation used in ActivityPub](https://github.com/AmaseCocoa/apsig) | ||
| - [apkit: Powerful Toolkit for ActivityPub Implementations](https://github.com/AmaseCocoa/apkit) | ||
| ## Links | ||
| - [Official Fedi Account](https://hollo.amase.cc/@apkit) | ||
|
|
||
| [](https://pypi.org/project/apmodel) | ||
| [](https://github.com/fedi-libs/apmodel/actions/workflows/test.yml) | ||
| [](https://results.pre-commit.ci/latest/github/fedi-libs/apmodel/develop) | ||
|
|
||
| apmodel is a Python library that provides model implementations for various decentralized social web protocols. | ||
|
|
||
| It is designed to easily parse and handle JSON data from sources like Mastodon, Misskey, and other Fediverse software. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Automatic Model Resolution**: The `apmodel.load` function automatically deserializes a JSON object into the appropriate Python object by reading its `type` field. If no matching model is found, the original dictionary is returned. | ||
| - **Flexible Deserialization**: Any properties in the JSON that do not have a corresponding field in the model are collected into an `model_extra` dictionary. This ensures no data is lost. | ||
| - **Type Hinting**: Fully type-hinted for a better development experience and robust static analysis. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install apmodel | ||
|
|
||
| # uv | ||
| uv add apmodel | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Here is a basic example of how to parse an ActivityStreams 2.0 `Note` object from a JSON string. | ||
|
|
||
| ```python | ||
| import apmodel | ||
|
|
||
| # Example JSON from a Fediverse server | ||
| json_data = { | ||
| "@context": "https://www.w3.org/ns/activitystreams", | ||
| "id": "https://example.com/users/alice/statuses/1", | ||
| "type": "Note", | ||
| "published": "2023-12-25T12:00:00Z", | ||
| "attributedTo": "https://example.com/users/alice", | ||
| "content": "<p>Hello, world!</p>", | ||
| "to": ["https://www.w3.org/ns/activitystreams#Public"], | ||
| "cc": ["https://example.com/users/alice/followers"] | ||
| } | ||
|
|
||
| # Load the JSON into an apmodel object | ||
| obj = apmodel.load(json_data) | ||
|
|
||
| # Now you can access properties with type safety | ||
| if isinstance(obj, apmodel.vocab.Note): | ||
| print(f"Type: {obj.type}") | ||
| print(f"Content: {obj.content}") | ||
| print(f"Published: {obj.published}") | ||
|
|
||
| # >> Type: Note | ||
| # >> Content: <p>Hello, world!</p> | ||
| # >> Published: 2023-12-25 12:00:00+00:00 | ||
| ``` | ||
|
|
||
| ## Documentation | ||
|
|
||
| https://fedi-libs.github.io/apmodel | ||
|
|
||
| ## Supported Specifications | ||
|
|
||
| `apmodel` provides models for the following specifications: | ||
|
|
||
| - **Activity Streams 2.0**: Core types like `Object`, `Activity`, `Collection`, and `Link`. | ||
| - **Activity Vocabulary**: All types. | ||
| - **Security Vocabulary v1**: `CryptographicKey` (`Key`). | ||
| - **Controlled Identifiers v1.0**: `Multikey` and `DataIntegrityProof`. | ||
| - **schema.org**: `PropertyValue`. | ||
| - **NodeInfo 2.0/2.1** | ||
| - **Litepub**: `EmojiReact` | ||
| - **Others**: `Emoji` and `Hashtag` | ||
|
|
||
| ## Development | ||
|
|
||
| This project uses [Task](https://taskfile.dev/) for running scripts. You need to install it first. | ||
|
|
||
| - **Setup dev environment**: | ||
|
|
||
| ```bash | ||
| task | ||
| ``` | ||
|
|
||
| - **Run tests with Pytest**: | ||
|
|
||
| ```bash | ||
| task test | ||
| ``` | ||
|
|
||
| - **Build packages for PyPI**: | ||
|
|
||
| ```bash | ||
| task build | ||
| ``` | ||
|
|
||
| - **Development Document**: | ||
| ```bash | ||
| task docs:dev | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| MIT License |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Core Types | ||
|
|
||
| This section documents the fundamental building blocks of the ActivityStreams models in `apmodel`. | ||
|
|
||
| ::: apmodel.core.object.Object | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.activity.Activity | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.activity.IntransitiveActivity | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.link.Link | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.collection.Collection | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.collection.OrderedCollection | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.collection.CollectionPage | ||
| options: | ||
| show_root_heading: true | ||
|
|
||
| ::: apmodel.core.collection.OrderedCollectionPage | ||
| options: | ||
| show_root_heading: true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.