Skip to content
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

Revisit custom id field types and type: ignore[assignment] #7

Open
felddy opened this issue Oct 15, 2024 · 0 comments
Open

Revisit custom id field types and type: ignore[assignment] #7

felddy opened this issue Oct 15, 2024 · 0 comments
Labels
improvement This issue or pull request will add or improve functionality, maintainability, or ease of use need info This issue or pull request requires further information on hold This pull request is awaiting an action or decision to move forward

Comments

@felddy
Copy link
Member

felddy commented Oct 15, 2024

💡 Summary

We've added # type: ignore[assignment] directives to the id fields of our models that use types other than ObjectId. In the case of Beanie the type is actually PydanticObjectId.

This directive is used in the following models:

  • CVEDoc
  • HostDoc
  • KEVDoc
  • PlaceDoc
  • RequestDoc
  • TallyDoc

Motivation and context

The following code, when used in a beanie Document will generate the type error below:

class TallyDoc(Document):
    counts: Counts = Field(default_factory=Counts)
    id: str  # owner_id
    last_change: datetime = Field(default_factory=utcnow)
➤ mypy src/
src/cyhy_db/models/tally_doc.py:42: error: Incompatible types in assignment (expression has type "str", base class "Document" defined the type as "PydanticObjectId | None")  [assignment]

The guidance in BeanieODM/beanie#12 does not work. I expect this is due to the current state of typing in Beanie. There is an ongoing effort to remedy this: BeanieODM/beanie#1050

The work-around is to ignore the assignment:

class TallyDoc(Document):
    counts: Counts = Field(default_factory=Counts)
    id: str  # type: ignore[assignment]  # owner_id
    last_change: datetime = Field(default_factory=utcnow)

Once BeanieODM/beanie#1050 is resolved we should be able to remove the # type: ignore[assignment] directives. 🤞

See:

@felddy felddy added improvement This issue or pull request will add or improve functionality, maintainability, or ease of use need info This issue or pull request requires further information on hold This pull request is awaiting an action or decision to move forward labels Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement This issue or pull request will add or improve functionality, maintainability, or ease of use need info This issue or pull request requires further information on hold This pull request is awaiting an action or decision to move forward
Projects
None yet
Development

No branches or pull requests

1 participant