Skip to content

Conversation

@AmaseCocoa
Copy link
Member

resolve #9

@AmaseCocoa AmaseCocoa mentioned this pull request Dec 10, 2025
This was linked to issues Dec 19, 2025
@AmaseCocoa AmaseCocoa mentioned this pull request Dec 19, 2025
8 tasks
  This change refactors the model registry to use lazy loading. Instead
  of importing all model classes at startup, the registry now stores
  import paths as strings.

  Models are dynamically imported only when they are first accessed via
  registry.get(). This significantly reduces the initial import time and
  memory consumption of the apmodel package.
@AmaseCocoa
Copy link
Member Author

AmaseCocoa commented Dec 22, 2025

Todo

  • no type error in pyrefly

AmaseCocoa and others added 9 commits December 22, 2025 21:57
This commit resolves all type errors reported by pyrefly.

The following changes were made:
- tests/test_real_data.py: Adjusted assertions to correctly access model_extra attributes and check for None values for optional fields.
- tests/test_registry.py: Corrected type hints for MINIMAL_PRELOADS and CustomObject.type to align with expected types. Imported Optional.
- src/apmodel/_core/_initial/_rebuild.py: Updated type hint for models_to_rebuild to List[Type[ActivityPubModel]] and removed Nodeinfo related classes as they do not inherit ActivityPubModel. Also simplified the rebuild loop.
- src/apmodel/_core/key.py: Added ValueError for unsupported key types to ensure 'wrapped' is always initialized.
- src/apmodel/context.py: Removed __iter__ method from LDContext as it was inconsistent with BaseModel's __iter__.
- src/apmodel/core/collection.py: Used typing.cast to explicitly cast return values of load functions in field validators. Imported cast.
- src/apmodel/extra/litepub/emoji_react.py: Changed EmojiReact.content type to Optional[str] to match parent class Like. Imported Optional.
- src/apmodel/_core/_initial/_registry_bootstrap.py: Added explicit type hint for TYPE_MAPPING to ensure compatibility with ModelRegistry.
- src/apmodel/vocab/activity/question.py: Used typing.cast to explicitly cast return values of load functions in field validators. Imported cast.
- src/apmodel/vocab/tombstone.py: Used typing.cast to explicitly cast return values of load functions in field validators. Imported cast.
@AmaseCocoa
Copy link
Member Author

Check It later, last correctly working version...

c989921

AmaseCocoa and others added 7 commits December 23, 2025 12:28
items

  Updated Collection's first, current, last fields to explicitly include
  OrderedCollectionPage in their type hints, allowing Pydantic to
  correctly deserialize them as such.

  Refactored Collection and OrderedCollection to correctly handle
  ordered_items and items fields. Strings within these lists are now
  converted to Link objects, and dictionaries with a 'type' field are
  loaded as their respective models.

  Modified test_akkoma_replies to align with the corrected loading
  behavior of OrderedCollectionPage. Also updated various assert
  statements in test_real_data.py for better clarity and conciseness by
  replacing is not None with direct boolean evaluation where
  appropriate.
@AmaseCocoa
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This is an impressive pull request that undertakes a major migration from dataclasses to Pydantic. The new implementation is much more robust, leveraging Pydantic's validation and serialization capabilities effectively. The addition of comprehensive documentation, a lazy-loading model registry, and tests against real-world data from various Fediverse platforms are all fantastic improvements.

My review focuses on a few areas for potential refinement:

  • A correctness issue in cryptographic key encoding.
  • Opportunities to improve maintainability in the pre-commit configuration and the custom JSON-LD context handling logic.

Overall, this is a high-quality contribution that significantly modernizes the library's core.

AmaseCocoa and others added 6 commits December 23, 2025 13:04
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
  The serialization logic for the @context property was inconsistent. It
  would produce a string for a single context URL and a list for
  multiple URLs.

  This change ensures that @context is always serialized as a list,
  providing a predictable structure for consumers and simplifying
  client-side parsing.

  BREAKING CHANGE: The @context property is now always serialized as a
  list, even if it contains only a single item. Consumers that
  previously expected a string must now handle a list.
  This commit introduces the ActorEndpoints model, integrating it into
  the system's initial model rebuild process.

  Extensive new test suites have been added to enhance coverage and
  validate the functionality of several models, including:
   - Actor and its various subtypes
   - CryptographicKey, covering PEM and multibase key handling
   - DataIntegrityProof, including datetime conversions and
     serialization
   - Multikey, with multibase encoding/decoding for public and private
     keys
   - Question, validating fields like oneOf, anyOf, and closed
   - Tombstone, verifying formerType and deleted fields
   - ActivityPubModel, testing base class features and context
     aggregation
   - Core key utility functions for multibase encoding/decoding

  Additionally, the datetime serialization in the Question model has
  been updated to consistently use the 'Z' suffix for UTC offsets,
  ensuring standard compliance and predictability. This also includes
  validation for the recently adjusted @context serialization behavior.
@AmaseCocoa AmaseCocoa merged commit d8d9261 into develop Dec 23, 2025
10 checks passed
@AmaseCocoa AmaseCocoa deleted the next branch December 23, 2025 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate to pydantic Better loader Deprecate Undefined

2 participants