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

ForeignKey static typing #914

Merged
merged 5 commits into from
Dec 22, 2023
Merged

ForeignKey static typing #914

merged 5 commits into from
Dec 22, 2023

Conversation

dantownsend
Copy link
Member

@dantownsend dantownsend commented Dec 22, 2023

Resolves #680

Related to #436

ForeignKey static typing in joins

The 'holy grail' for Piccolo for a long time has been to get 'perfect' static typing for ForeignKey columns.

If there was some kind of Proxy type in the typing module, it would be easy, but there isn't. Here's a related issue (python/typing#802) for anyone who wants to track the progress.

I've come up with a solution. This query will continue to work:

await Band.select(Band.name, Band.manager.name)

But this PR allows you to do this:

await Band.select(Band.name, Band.manager._.name)

Note the addition of the _ after the foreign key. It then has perfect static typing.

To remember the syntax, ._. looks a bit like a connector in a diagram.

When Python adds a Proxy type to the typing module, we can make Band.manager.name behave the same as Band.manager._.name, but for now I think it's the best solution.

With perfect static typing of foreign keys, it is really cool, because Mypy and Pylance will detect any column name typos when doing joins.

get_related

This PR also improves the typing for get_related. For example:

band = await Band.objects().first()

# Oops, I need the manager object too:
manager = await band.get_related(Band.manager)

In the above example, the type for manager is Manager | None.

Remaining tasks

  • Fix some minor linter errors
  • Add some tests to make sure the new syntax works
  • Update the docs

@dantownsend dantownsend added the enhancement New feature or request label Dec 22, 2023
@codecov-commenter
Copy link

codecov-commenter commented Dec 22, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (4fb169d) 87.91% compared to head (92fb265) 91.99%.

Files Patch % Lines
piccolo/columns/column_types.py 82.35% 3 Missing ⚠️
piccolo/table.py 71.42% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #914      +/-   ##
==========================================
+ Coverage   87.91%   91.99%   +4.07%     
==========================================
  Files         107      107              
  Lines        8145     8165      +20     
==========================================
+ Hits         7161     7511     +350     
+ Misses        984      654     -330     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dantownsend dantownsend merged commit d05c2da into master Dec 22, 2023
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Foreign keys typing support
2 participants