Draft: Improve typing for github_user attributes on GraphQL nodes#5100
Draft: Improve typing for github_user attributes on GraphQL nodes#5100Tanishq-mellu wants to merge 1 commit into
Conversation
Signed-off-by: Tanishq Meshram <tnshqmeshram@gmail.com>
|
Contribution validation failed:
|
|
Summary by CodeRabbit
WalkthroughThree GraphQL node types ( ChangesGitHubUser field on GraphQL nodes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@backend/src/apps/mentorship/api/internal/nodes/admin.py`:
- Around line 7-15: The AdminNode model is exposing an internal github_user
attribute as a public GraphQL field, and its TYPE_CHECKING-only annotation
causes a NameError at class definition time. Update AdminNode to keep the
backing GitHub user reference private by changing the github_user attribute to
Strawberry private storage using strawberry.Private with the GitHubUser | None
type, so the field is not part of the GraphQL schema and no runtime lookup of
GitHubUser is required.
In `@backend/src/apps/mentorship/api/internal/nodes/mentor.py`:
- Around line 7-15: The MentorNode class is referencing GitHubUser at runtime
even though it is only imported under TYPE_CHECKING, which causes the class body
to fail when evaluated. Update MentorNode to treat github_user as an
internal-only field by using a Strawberry private attribute, and keep the symbol
names MentorNode and github_user as the place to make the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: bd14916c-b251-4d30-8a7e-0f0e9861cf9c
📒 Files selected for processing (3)
backend/src/apps/mentorship/api/internal/nodes/admin.pybackend/src/apps/mentorship/api/internal/nodes/mentor.pybackend/src/apps/nest/api/internal/nodes/user.py
There was a problem hiding this comment.
4 issues found across 3 files
Confidence score: 2/5
- In
backend/src/apps/nest/api/internal/nodes/user.pyandbackend/src/apps/mentorship/api/internal/nodes/mentor.py,GitHubUseris only imported underTYPE_CHECKINGbut referenced in runtime-evaluated annotations, which can triggerNameErrorduring import/schema resolution and break API startup—make the type available at runtime (or defer/quote the annotation) before merging. - In
backend/src/apps/mentorship/api/internal/nodes/admin.pyandbackend/src/apps/mentorship/api/internal/nodes/mentor.py,github_useris currently treated as a public Strawberry field, so internal model data could leak into the GraphQL schema or cause type-mapping issues—mark it as a Strawberry private field (or otherwise exclude it from schema generation) before merging.
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
Hi maintainers! Thanks for taking a look. This is my first contribution to the typing improvements tracked in #5080, so I wanted to open it as a draft to validate the approach before making similar changes elsewhere. The goal of this PR is only to resolve the attr-defined mypy errors by explicitly annotating the github_user attribute on the affected Strawberry node types. I intentionally kept the scope small so it's easier to review. If this isn't the preferred pattern, I'd really appreciate guidance on the recommended approach. I'm happy to revise the implementation or explore an alternative solution based on your feedback. Also, I noticed the automated review comments regarding runtime annotations and Strawberry private fields. I haven't addressed those yet because I'd first like to confirm the intended direction with the maintainers rather than making assumptions. |
arkid15r
left a comment
There was a problem hiding this comment.
https://github.com/OWASP/Nest/blob/main/CONTRIBUTING.md#contributing-workflow
Also make it holistic, if you fix a class of issues in the code I expect some pyproject.toml changes as well.
|
Thanks for the clarification. I investigated the scope by temporarily removing the attr-defined suppression locally. That exposes 18 attr-defined errors across ProgramNode, ModuleNode and MilestoneQuery, many of which are related to Strawberry-Django model-backed nodes. I'll work on addressing the issue holistically and update the corresponding pyproject.toml configuration as part of the fix. If there's a preferred typing pattern for these Strawberry-Django nodes that you'd like me to follow, please let me know; otherwise I'll proceed with the approach that removes the need for the suppression. |



Refs #5080
Summary
This draft PR improves type information for several GraphQL node classes by making the
github_userattribute visible to mypy.Several resolver methods access
github_user, but the attribute is not declared on the corresponding Strawberry types, which results inattr-definederrors during static type checking.Changes
TYPE_CHECKINGimports forGitHubUsergithub_user: GitHubUser | Noneannotations to:AdminNodeMentorNodeAuthUserNodeValidation
pre-commit run mypy --all-filesFeedback requested
I'd appreciate feedback on whether this is the preferred pattern for exposing internal model attributes on Strawberry GraphQL node types while improving static typing.
Question for reviewers
Is explicitly annotating model-backed attributes on Strawberry node types the preferred approach for resolving these
attr-definedmypy errors, or would you recommend a different typing pattern?