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

[fastapi] Implement fast-api-unused-path-parameter (FAST003) #12638

Merged

Conversation

Matthieu-LAURENT39
Copy link
Contributor

@Matthieu-LAURENT39 Matthieu-LAURENT39 commented Aug 2, 2024

This adds the fast-api-unused-path-parameter lint rule, as described in #12632.

I'm still pretty new to rust, so the code can probably be improved, feel free to tell me if there's any changes i should make.

Also, i needed to add the add_parameter edit function, not sure if it was in the scope of the PR or if i should've made another one.

@Matthieu-LAURENT39 Matthieu-LAURENT39 changed the title Add fastapi-unused-path-parameter [fastapi] Implement fast-api-unused-path-parameter (FAST003) Aug 2, 2024
@Matthieu-LAURENT39 Matthieu-LAURENT39 marked this pull request as ready for review August 2, 2024 23:37
Copy link
Contributor

github-actions bot commented Aug 2, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thanks. Overall looking good. I have to review it a bit more closely but I left a few comments on how we can improve performance.

@Matthieu-LAURENT39
Copy link
Contributor Author

Matthieu-LAURENT39 commented Aug 3, 2024

Somehow VSCode didn't show the code blocks inside your reviews so i implemented that from scratch 😅
Works pretty well though

if let Some((end, _)) = self.chars.by_ref().find(|&(_, ch)| ch == '}') {
let param_content = &self.input[start + 1..end];
// We ignore text after a colon, since those are path convertors
// See also: https://fastapi.tiangolo.com/tutorial/path-params/?h=path#path-convertor
Copy link
Member

Choose a reason for hiding this comment

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

Since path parameters use the same format as f-strings, could we use our parser here instead? Just parse the path, extract the f-string segments? You can see here that its evaluated to a literal element, followed by an FStringExpressionElement where the : part is the FStringFormatSpec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Path parameters can use invalid identifier names. For example, /route/{path-name}, which the f-string parser doesn't support. However, we just ignore invalid identifiers in the rule anyways since fastapi normalizes them in an undocumented way, so i don't think this is a blocker to using that parser.
Also, fastapi doesn't handle the {name!r} and {name=} special syntaxes and would normalize them as name_r and name respectively, but this is a very obscure edge-case so it doesn't matter much. We do need to at least disable the rule if there is a conversion or debug_text in the f-string to avoid complaining about the wrong name though.
If those aren't a problem, i'll switch to the f-string parser

Copy link
Member

Choose a reason for hiding this comment

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

I leave this up to you @charliermarsh but cloning the AST, then parsing it seems very heavy weight.

Copy link
Member

Choose a reason for hiding this comment

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

Ok yeah, let's just skip the parser idea for now then.

Does FastAPI allow you to escape curly braces? Like /foo{{bar}}/ to represent a literal { and } in the path, as with f-strings?

Copy link
Contributor Author

@Matthieu-LAURENT39 Matthieu-LAURENT39 Aug 6, 2024

Choose a reason for hiding this comment

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

it doesn't, it will just be treated as if you had a path attribute named bar (without curly braces).
Although i'm pretty sure it's because of the aforementioned undocumented normalizing behavior.

Should i revert the commits using the f-string parser?

Copy link
Member

Choose a reason for hiding this comment

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

Should i revert the commits using the f-string parser?

Yeah I think that would be great, considering that fast-api doesn't support f-strings?

Copy link

codspeed-hq bot commented Aug 3, 2024

CodSpeed Performance Report

Merging #12638 will not alter performance

Comparing Matthieu-LAURENT39:fastapi_unused_path_parameter (288bd10) with main (80efb86)

Summary

✅ 32 untouched benchmarks

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Aug 13, 2024
@Matthieu-LAURENT39
Copy link
Contributor Author

reverted to the old parsing system that doesn't use the f-string parser, as per the conversation

@tiangolo
Copy link

Amazing! 🚀

@charliermarsh charliermarsh self-assigned this Aug 16, 2024
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Reviewing now!

@charliermarsh charliermarsh changed the title [fastapi] Implement fast-api-unused-path-parameter (FAST003) [fastapi] Implement fast-api-unused-path-parameter (FAST003) Aug 16, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) August 16, 2024 01:41
@charliermarsh charliermarsh merged commit f121f8b into astral-sh:main Aug 16, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants