--- /dev/null
+.. change::
+ :tags: bug, mypy
+ :tickets: 6205
+
+ Revised the fix for ``OrderingList`` from version 1.4.7 which was testing
+ against the incorrect API.
from mypy.nodes import AssignmentStmt
from mypy.nodes import CallExpr
from mypy.nodes import Expression
+from mypy.nodes import FuncDef
from mypy.nodes import MemberExpr
from mypy.nodes import NameExpr
from mypy.nodes import RefExpr
from mypy.plugin import SemanticAnalyzerPluginInterface
from mypy.subtypes import is_subtype
from mypy.types import AnyType
+from mypy.types import CallableType
from mypy.types import get_proper_type
from mypy.types import Instance
from mypy.types import NoneType
python_type_for_type = Instance(
collection_cls_arg.node, [python_type_for_type]
)
+ elif (
+ isinstance(collection_cls_arg, NameExpr)
+ and isinstance(collection_cls_arg.node, FuncDef)
+ and collection_cls_arg.node.type is not None
+ ):
+ if python_type_for_type is not None:
+ # this can still be overridden by the left hand side
+ # within _infer_Type_from_left_and_inferred_right
+
+ # TODO: handle mypy.types.Overloaded
+ if isinstance(collection_cls_arg.node.type, CallableType):
+ rt = get_proper_type(collection_cls_arg.node.type.ret_type)
+
+ if isinstance(rt, CallableType):
+ callable_ret_type = get_proper_type(rt.ret_type)
+ if isinstance(callable_ret_type, Instance):
+ python_type_for_type = Instance(
+ callable_ret_type.type,
+ [python_type_for_type],
+ )
else:
util.fail(
api,
[mypy-sqlalchemy.ext.mypy.*]
ignore_errors = False
+
[sqla_testing]
requirement_cls = test.requirements:DefaultRequirements
profile_file = test/profiles.txt
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
-from sqlalchemy.ext.orderinglist import OrderingList
+from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.orm import registry
from sqlalchemy.orm import relationship
id = Column(Integer, primary_key=True)
# EXPECTED: Can't infer type from ORM mapped expression assigned to attribute 'parents'; please specify a Python type or Mapped[<python type>] on the left hand side. # noqa
- parents = relationship("A", collection_class=OrderingList("ordering"))
+ parents = relationship("A", collection_class=ordering_list("ordering"))
parent_id = Column(Integer, ForeignKey("a.id"))
ordering = Column(Integer)
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
-from sqlalchemy.ext.orderinglist import OrderingList
+from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.orm import registry
from sqlalchemy.orm import relationship
__tablename__ = "a"
id = Column(Integer, primary_key=True)
- bs = relationship(B, collection_class=OrderingList("ordering"))
+ bs = relationship(B, collection_class=ordering_list("ordering"))
bs_w_list: List[B] = relationship(
- B, collection_class=OrderingList("ordering")
+ B, collection_class=ordering_list("ordering")
)
# EXPECTED: Left hand assignment 'cs: "List[B]"' not compatible with ORM mapped expression of type "Mapped[List[C]]" # noqa
b1 = B(ordering=10)
# in this case, the plugin infers OrderingList as the type. not great
-a1 = A(bs=OrderingList(b1))
+a1 = A()
+a1.bs.append(b1)
# so we want to support being able to override it at least
a2 = A(bs_w_list=[b1])