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

feat: support declaring DTOField via Annotated #2367

Closed
wants to merge 6 commits into from

Conversation

peterschutt
Copy link
Contributor

E.g.:

class A(Struct):
    a: Annotated[int, dto_field("read_only")]

For #2351

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR

Description

Close Issue(s)

@peterschutt peterschutt force-pushed the 2351-dto-field-via-annotated branch 2 times, most recently from c9b14f4 to 8dbf980 Compare September 26, 2023 11:21
@peterschutt peterschutt changed the title Feat: support declaring DTOField via Annotated feat: support declaring DTOField via Annotated Sep 26, 2023
@peterschutt peterschutt force-pushed the 2351-dto-field-via-annotated branch from 8dbf980 to 907ebd5 Compare September 27, 2023 05:24
@provinzkraut
Copy link
Member

@peterschutt this seems to be ready?

@peterschutt
Copy link
Contributor Author

@peterschutt this seems to be ready?

Yeh close - no tests. I have some related changes to PR, however I'm AFK for the weekend.

@peterschutt peterschutt force-pushed the 2351-dto-field-via-annotated branch 2 times, most recently from b0472a4 to 1e8a24f Compare October 11, 2023 03:22
@peterschutt
Copy link
Contributor Author

If an argument to Annotated is not hashable, e.g., Annotated[int, {"set"}], then msgspec.inspect.type_info() throws a TypeError.

I created jcrist/msgspec#565 for that, however, for the tests to pass we'd either need to wait for that to make a release, or make DTOField hashable. I've chosen to do the latter. Any objections @litestar-org/maintainers?

@peterschutt peterschutt marked this pull request as ready for review October 11, 2023 03:56
@peterschutt peterschutt requested review from a team as code owners October 11, 2023 03:56
litestar/dto/field.py Outdated Show resolved Hide resolved
@JacobCoffee
Copy link
Member

Is that unsafe_hash needed for msgpsec?

@peterschutt
Copy link
Contributor Author

Is that unsafe_hash needed for msgpsec?

Yep. Msgspec passes the type into dict.get() which calls _AnnotatedAlias.__hash__() which in turn passes a tuple of the instance and its metadata to hash(). This means that for Annotated to be hashable, all of its metadata must be hashable, and DTOField isn't.

I ended up going with unsafe_hash=True as I figure it would be less imposing than making the type eq=True, frozen=True. Having said that though, these things are pretty much designed to be instantiated in the global namespace (i.e., at model definition time), and it would be weird to modify them after the fact (although we do modify them to mark read-only et al. based on the DTOConfig configuration). So, I'd prefer to make them truly immutable and safe hash, but not sure if that would be too backward incompatible.

The other option is to block this until msgspec cuts their next release and not modify the hashability of the types at all.

@peterschutt
Copy link
Contributor Author

The other option is to block this until msgspec cuts their next release and not modify the hashability of the types at all.

FYI: waiting for this.

@peterschutt peterschutt force-pushed the 2351-dto-field-via-annotated branch from 52cf62c to dbb8e8a Compare November 1, 2023 00:38
@provinzkraut provinzkraut added the Blocked 🚫 This is blocked by something label Nov 1, 2023
@peterschutt peterschutt self-assigned this Dec 1, 2023
@peterschutt peterschutt added the Enhancement This is a new feature or request label Dec 1, 2023
Copy link

sonarcloud bot commented Dec 2, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

91.3% 91.3% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

Copy link

github-actions bot commented Dec 2, 2023

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/2367

provinzkraut and others added 2 commits December 3, 2023 12:46
* wip

Signed-off-by: Janek Nouvertné <[email protected]>

* some debugging

Signed-off-by: Janek Nouvertné <[email protected]>

* formatting

Signed-off-by: Janek Nouvertné <[email protected]>

* use a separate connection to publish/listen

Signed-off-by: Janek Nouvertné <[email protected]>

* reintroduce flaky

Signed-off-by: Janek Nouvertné <[email protected]>

* Fix typing

Signed-off-by: Janek Nouvertné <[email protected]>

* Add psycopg backend

Signed-off-by: Janek Nouvertné <[email protected]>

* Fix backend issues

Signed-off-by: Janek Nouvertné <[email protected]>

* Undo test debugging changes

Signed-off-by: Janek Nouvertné <[email protected]>

* mark groups

Signed-off-by: Janek Nouvertné <[email protected]>

* Ensure channel names ar quoted

Signed-off-by: Janek Nouvertné <[email protected]>

* sleep debugging

Signed-off-by: Janek Nouvertné <[email protected]>

* update docs

Signed-off-by: Janek Nouvertné <[email protected]>

* Add missing test

Signed-off-by: Janek Nouvertné <[email protected]>

* Fix docs link

Signed-off-by: Janek Nouvertné <[email protected]>

* Add missing listener test

Signed-off-by: Janek Nouvertné <[email protected]>

* Formatting

Signed-off-by: Janek Nouvertné <[email protected]>

* Fix test typing

Signed-off-by: Janek Nouvertné <[email protected]>

* Fix some coverage issue

Signed-off-by: Janek Nouvertné <[email protected]>

---------

Signed-off-by: Janek Nouvertné <[email protected]>
Co-authored-by: Cody Fincher <[email protected]>
Signed-off-by: Janek Nouvertné <[email protected]>
@provinzkraut provinzkraut changed the base branch from main to develop December 3, 2023 14:17
@provinzkraut provinzkraut force-pushed the 2351-dto-field-via-annotated branch from d022890 to 2a0ece5 Compare December 3, 2023 14:17
E.g.:

```py
class A(Struct):
    a: Annotated[int, dto_field("read_only")]
```

For #2351
@provinzkraut provinzkraut force-pushed the develop branch 4 times, most recently from de19eed to 9749d94 Compare January 27, 2024 12:35
@provinzkraut provinzkraut force-pushed the develop branch 4 times, most recently from 47bd532 to 1e037d3 Compare February 6, 2024 17:32
@provinzkraut provinzkraut force-pushed the develop branch 5 times, most recently from 1e2f5cd to 37c59b7 Compare February 14, 2024 14:06
@provinzkraut provinzkraut force-pushed the develop branch 2 times, most recently from 6341f9b to 122448b Compare March 4, 2024 09:57
@provinzkraut provinzkraut force-pushed the develop branch 3 times, most recently from 353ebca to 525cd4c Compare March 16, 2024 09:07
@provinzkraut provinzkraut deleted the branch develop March 18, 2024 17:08
@provinzkraut provinzkraut reopened this Mar 18, 2024
@provinzkraut provinzkraut force-pushed the develop branch 2 times, most recently from 8ede1b5 to 03b63e6 Compare March 29, 2024 15:42
@provinzkraut
Copy link
Member

@peterschutt Anything blocking this aside from time constraints?

@peterschutt
Copy link
Contributor Author

@peterschutt Anything blocking this aside from time constraints?

Nothing - except that I keep getting distracted with work closer to the top of the pile.

@peterschutt
Copy link
Contributor Author

Superseded by #3289 3289

@peterschutt peterschutt deleted the 2351-dto-field-via-annotated branch April 11, 2024 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants