You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Due to the interest in #16864 in the rules I proposed in miketheman/flake8-sqlalchemy#54 (but were never implemented), I created this draft to add the first SQLAlchemy rule.
See changes in crates/ruff_linter/src/rules/sqlalchemy/rules/missing_mapped_type_annotation.rs for the motivation of this rule.
Implementation and Limitations
Currently, it supports most commonly used column mappings (mapped_column, relationship, ...), but still lacking support for e.g. composite.
Auto fixes might be possible in some cases, but can be tricky because it would require an understanding of type_annotation_map.
The mapped_column() construct in modern Python is normally augmented by the use of PEP 484 Python type annotations, where it is capable of deriving its column-configuration information from type annotations associated with the attribute as declared in the Declarative mapped class. These type annotations, if used, must be present within a special SQLAlchemy type called Mapped, which is a generic type that indicates a specific Python type within it.
Both forms are used in the examples ( tagged as Annotated Example and Non-Annotated Example).
The Non-Annotated form is also a natural migration step to SQLAlchemy 2.0:
Users of 1.x SQLAlchemy will note the use of the mapped_column() construct, which is new as of the SQLAlchemy 2.0 series. This ORM-specific construct is intended first and foremost to be a drop-in replacement for the use of Column within Declarative mappings only, adding new ORM-specific convenience features such as the ability to establish mapped_column.deferred within the construct, and most importantly to indicate to typing tools such as Mypy and Pylance an accurate representation of how the attribute will behave at runtime at both the class level as well as the instance level. As will be seen in the following sections, it’s also at the forefront of a new annotation-driven configuration style introduced in SQLAlchemy 2.0.
In a codebase I am working with, in some places the annotations were missing (for whatever reason). We were not aware that these columns were treated as Any causing some bugs in the past.
I'd suggest collecting all the SQAlchemy 2.0 specific rules into the SA2 prefix, so making this SA201 rather than SA001. That way we leave the door open to put generally useful rules into SA0 and legacy rules into SA1, making it easier to read through the rules once there are more of them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
needs-decisionAwaiting a decision from a maintainerruleImplementing or modifying a lint rule
4 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Due to the interest in #16864 in the rules I proposed in miketheman/flake8-sqlalchemy#54 (but were never implemented), I created this draft to add the first
SQLAlchemyrule.See changes in
crates/ruff_linter/src/rules/sqlalchemy/rules/missing_mapped_type_annotation.rsfor the motivation of this rule.Implementation and Limitations
Currently, it supports most commonly used column mappings (
mapped_column,relationship, ...), but still lacking support for e.g.composite.Auto fixes might be possible in some cases, but can be tricky because it would require an understanding of
type_annotation_map.Test Plan
SA001.pysnapshot test was added.