Switch to Python 3.9+ type annotation syntax everywhere#539
Switch to Python 3.9+ type annotation syntax everywhere#539NobleCoder69 wants to merge 1 commit intoneurobagel:mainfrom
Conversation
Reviewer's GuideRefactors type annotations to use Python 3.9+ syntax (PEP 585 built-in generics and PEP 604 unions) across API models and utility helpers, replacing Optional/Union and untyped collections with the newer Class diagram for updated API response and query modelsclassDiagram
class QueryModel {
+float min_age
+float max_age
+str sex
+str diagnosis
+int min_num_imaging_sessions
+int min_num_phenotypic_sessions
+str assessment
+str image_modal
+str pipeline_name
+str pipeline_version
+check_maxage_ge_minage() QueryModel
}
class SessionResponse {
+str subject_id
+str dataset_uuid
+int num_matching_phenotypic_sessions
+int num_matching_imaging_sessions
+str session_type
+float age
+str sex
+list diagnosis
+str subject_group
+list assessment
+list image_modal
+str session_file_path
+dict completed_pipelines
}
class CohortQueryResponse {
+str dataset_uuid
+str dataset_name
+str dataset_portal_uri
+int dataset_total_subjects
+bool records_protected
+int num_matching_subjects
+list image_modals
+dict available_pipelines
+list~SessionResponse~ subject_data
}
class DatasetQueryResponse {
+str dataset_uuid
+str dataset_name
+list~str~ authors
+str homepage
+list~str~ references_and_links
+list~str~ keywords
+str repository_url
+str access_instructions
+str access_type
+str access_email
+str access_link
+int dataset_total_subjects
+bool records_protected
+int num_matching_subjects
}
class SubjectsQueryResponse {
+str dataset_uuid
+list~SessionResponse~ subject_data
}
class StandardizedTermVocabularyNamespace {
+str vocabulary_name
+str namespace_url
+str namespace_prefix
+str version
+list~dict~ terms
}
CohortQueryResponse "*" o-- "*" SessionResponse : uses
SubjectsQueryResponse "*" o-- "*" SessionResponse : uses
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Pull request overview
Migrates type annotations to Python 3.9+ syntax (built-in generics and PEP 604 unions) across API utilities and Pydantic models.
Changes:
- Replaced
Optional[...]/Union[...]withT | NoneandA | B. - Began shifting to built-in generic aliases (e.g.,
list[...]). - Updated model field annotations to reflect optionality more explicitly.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| app/api/utility.py | Updates create_query parameter type hints to PEP 604 unions and removes Optional import. |
| app/api/models.py | Converts Pydantic model field annotations from Optional/Union to ` |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| from enum import Enum | ||
| from typing import Optional, Union | ||
| # remove this line completely |
There was a problem hiding this comment.
This looks like a temporary PR note that was accidentally committed into the source. Please remove this comment and, if needed, replace it with the actual intended change (i.e., delete the unused typing import line rather than leaving an instruction in code).
| # remove this line completely |
| pipeline_name: Optional[str] = None, | ||
| pipeline_version: Optional[str] = None, | ||
| dataset_uuids: Optional[list] = None, | ||
| age: tuple | None = (None, None), |
There was a problem hiding this comment.
These annotations are very broad (tuple and list without element types), which weakens type checking and IDE support. Consider specifying element types (e.g., tuple[float | None, float | None] | None for age and list[str] | None for dataset_uuids, if those match the actual expected values).
| image_modal: str | None = None, | ||
| pipeline_name: str | None = None, | ||
| pipeline_version: str | None = None, | ||
| dataset_uuids: list | None = None, |
There was a problem hiding this comment.
These annotations are very broad (tuple and list without element types), which weakens type checking and IDE support. Consider specifying element types (e.g., tuple[float | None, float | None] | None for age and list[str] | None for dataset_uuids, if those match the actual expected values).
| sex: str | None = Field(default=None, pattern=CONTROLLED_TERM_REGEX, examples=["vocab:12345"]) | ||
| diagnosis: str | None = Field(default=None, pattern=CONTROLLED_TERM_REGEX, examples=["vocab:12345"]) | ||
| min_num_imaging_sessions: int | None = Field(default=None, ge=0) | ||
| min_num_phenotypic_sessions: int | None = Field(default=None, ge=0) | ||
| assessment: str | None = Field(default=None, pattern=CONTROLLED_TERM_REGEX, examples=["vocab:12345"]) | ||
| image_modal: str | None = Field(default=None, pattern=CONTROLLED_TERM_REGEX, examples=["vocab:12345"]) | ||
| pipeline_name: str | None = Field(default=None, pattern=CONTROLLED_TERM_REGEX, examples=["vocab:12345"]) | ||
| pipeline_version: str | None = Field(default=None, pattern=VERSION_REGEX, examples=["1.0.0"]) |
There was a problem hiding this comment.
These lines are quite long and harder to scan/review. Consider formatting these Field(...) calls across multiple lines (as they were previously) or running the project formatter (e.g., Black) so arguments wrap consistently.
|
Hi @alyssadai, This PR is intended to resolve issue #518 by migrating the type annotations to Python 3.9+ syntax across the repository. Please let me know if any changes are required. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #539 +/- ##
==========================================
- Coverage 97.16% 97.16% -0.01%
==========================================
Files 34 34
Lines 1304 1303 -1
==========================================
- Hits 1267 1266 -1
Misses 37 37 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Replaced legacy typing syntax (Optional, Union, List, etc.)
with Python 3.9+ built-in generics and PEP 604 union types.
Examples:
All tests passing (145/145).
Summary by Sourcery
Modernize type annotations to use Python 3.9+ syntax across API models and utility helpers.
Enhancements: