1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
U
¸ý°d÷Mã    @s:ddlmZddlmZddlmZddlmZddlmZddl    m
Z
ddl    m Z ddl    m Z dd    l    m Z dd
l    mZdd l    mZdd l    mZdd l    mZddl    mZddl    mZddl    mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddl"m#Z#ddl"m$Z$ddd d!d"d!d#œd$d%„Z%ddd d!d!d&œd'd(„Z&ddd d!d!d&œd)d*„Z'ddd d!d"d!d#œd+d,„Z(ddd d!d!d&œd-d.„Z)dDddd d!d0d!d1œd2d3„Z*dEdd d4d4d!d!d!d5œd6d7„Z+dd d8d8d!d9œd:d;„Z,dd d!d!d<œd=d>„Z-dd?d@d4dAœdBdC„Z.d/S)Fé)Ú annotations)ÚOptional)ÚSequence)Úmap_instance_to_supertype)Ú format_type)ÚAssignmentStmt)ÚCallExpr)Ú
Expression)ÚFuncDef)Ú
LambdaExpr)Ú
MemberExpr)ÚNameExpr)ÚRefExpr)ÚStrExpr)ÚTypeInfo)ÚVar)ÚSemanticAnalyzerPluginInterface)Ú
is_subtype)ÚAnyType)Ú CallableType)Úget_proper_type)ÚInstance)ÚNoneType)Ú
ProperType)Ú    TypeOfAny)Ú    UnionTypeé)Únames)ÚutilrrrzOptional[ProperType]r)ÚapiÚstmtÚnodeÚleft_hand_explicit_typeÚinfer_from_right_sideÚreturncCsºt |¡}|dkrdS|tjkr2t|||||ƒ}n„|tjkrLt||||ƒ}nj|tjkrft||||ƒ}nP|tjkr€t    ||||ƒ}n6|tj
kr˜t |||ƒ}n|tj kr²t ||||ƒ}ndS|S)N)rÚtype_id_for_calleeZMAPPEDÚ_infer_type_from_mappedÚCOLUMNÚ_infer_type_from_decl_columnZ RELATIONSHIPÚ_infer_type_from_relationshipZCOLUMN_PROPERTYÚ%_infer_type_from_decl_column_propertyZSYNONYM_PROPERTYÚ#infer_type_from_left_hand_type_onlyZCOMPOSITE_PROPERTYÚ(_infer_type_from_decl_composite_property)rr r!r"r#Útype_idÚpython_type_for_type©r/úPd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/ext/mypy/infer.pyÚ#infer_type_from_right_hand_nameexpr)s^
 
ÿ
ÿ
ÿ
ÿ
ÿ
ÿr1)rr r!r"r$c Cs„t|jtƒst‚|jjd}d}t|tƒrFt|jtƒrF|j}t|gƒ}t     
|jd¡}t     
|jd¡}d}    |dk    r¤|  |¡dkr¤|dkr¤d}    |dk    r |  t j|g¡}nt|dks¼|  |¡dkr¦|dk    r¦d}    t|tƒrÚ|j}t|tƒrt|jtƒr|dk    r¤t|j|gƒ}n–t|tƒrt|jtƒr|jjdk    r|dk    r¤t|jjtƒr¤t|jjjƒ}
t|
tƒr¤t|
jƒ} t| tƒr¤t| j|gƒ}nt     |d|j¡d}nr|dk    rô|  |¡dkrô|dk    rÚt     |d|j¡|dk    rt|tƒgƒ}n$|dkrd    } t     ||  |j¡|¡|dkr.t|||ƒS|dk    r||    rlt|tƒsNt‚t|tƒs^t‚t||||ƒSt||||ƒSn|SdS)
a¦Infer the type of mapping from a relationship.
 
    E.g.::
 
        @reg.mapped
        class MyClass:
            # ...
 
            addresses = relationship(Address, uselist=True)
 
            order: Mapped["Order"] = relationship("Order")
 
    Will resolve in mypy as::
 
        @reg.mapped
        class MyClass:
            # ...
 
            addresses: Mapped[List[Address]]
 
            order: Mapped["Order"]
 
    rNZuselistZcollection_classFTz>Expected Python collection type for collection_class parameterzOSending uselist=False and collection_class at the same time does not make sensezäCan't infer scalar or collection for ORM mapped expression assigned to attribute '{}' if both 'uselist' and 'collection_class' arguments are absent from the relationship(); please specify a type annotation on the left hand side.)Ú
isinstanceÚrvaluerÚAssertionErrorÚargsr r!rrrZget_callexpr_kwargZ
parse_boolÚ
named_typerZNAMED_TYPE_BUILTINS_LISTÚcalleer
ÚtyperrZret_typeÚfailrrÚformatÚnamer+Ú3_infer_collection_type_from_left_and_inferred_rightÚ(_infer_type_from_left_and_inferred_right) rr r!r"Útarget_cls_argr.Úrelated_object_typeZ uselist_argZcollection_cls_argZtype_is_a_collectionÚrtZcallable_ret_typeÚmsgr/r/r0r)QsÒ  ÿ
ÿÿ þýÿÿ ÿþ
ÿ
ÿÿ
þ
ý
 
 þü
ü
ÿ
ÿ
ÿ
ÿür)cCs~t|jtƒst‚|jjd}d}t|tƒrHt|jtƒrH|j}t|gƒ}nd}|dkr`t    |||ƒS|dk    rvt
||||ƒS|SdS)z+Infer the type of mapping from a Composite.rN) r2r3rr4r5r r!rrr+r=)rr r!r"r>r.r?r/r/r0r,÷s0  ÿ ÿÿr,cCs&t|jtƒst‚t |¡}t|||ƒS)zVInfer the type of mapping from a right side expression
    that returns Mapped.
 
 
    )r2r3rr4rZtype_for_calleer+)rr r!r"r#Zthe_mapped_typer/r/r0r&s 
ÿr&cCs”t|jtƒst‚|jjrV|jjd}t|tƒrVt |j¡}|tjkrVt    |||||dSt|jtƒrˆt |jj¡}|tj
krˆt    ||||ƒSt |||ƒS)zœInfer the type of mapping from a ColumnProperty.
 
    This includes mappings against ``column_property()`` as well as the
    ``deferred()`` function.
 
    r)Úright_hand_expression) r2r3rr4r5rr%r7r'r(ZQUERY_EXPRESSIONr+)rr r!r"Zfirst_prop_argr-r/r/r0r*7s6  
 
û 
üÿr*NzOptional[CallExpr])rr r!r"rBr$c    Cs4t|tƒst‚d}|dkr0t|jtƒs*dS|j}|jdd…D]€}t|tƒrjt|jtƒr¾|j}|j}qÀq>t|tt    fƒr–t|j
t ƒr>|}d}qÀq¾q>q>t|t fƒr¦q>q>t|t fƒr¶q>q>ds>t‚q>|dkrÌdSt|j
t ƒr$t |j
jtj¡r$t||j
|ƒ}|dk    rt||||ƒSt|tƒgƒSn t|||ƒSdS)aãInfer the type of mapping from a Column.
 
    E.g.::
 
        @reg.mapped
        class MyClass:
            # ...
 
            a = Column(Integer)
 
            b = Column("b", String)
 
            c: Mapped[int] = Column(Integer)
 
            d: bool = Column(Boolean)
 
    Will resolve in MyPy as::
 
        @reg.mapped
        class MyClass:
            # ...
 
            a : Mapped[int]
 
            b : Mapped[str]
 
            c: Mapped[int]
 
            d: Mapped[bool]
 
    Nrér/F)r2rr4r3rr5r7rr r r!rrr rZ
mro_has_idÚmroZ
TYPEENGINEÚ#extract_python_type_from_typeenginer=rrr+)    rr r!r"rBr7Z
column_argÚ    type_argsr.r/r/r0r(hs^& 
 
ÿÿ
ÿÿr(r)rr!r"r.Úorig_left_hand_typeÚorig_python_type_for_typer$c    Cs\|dkr |}|dkr|}t||ƒsX| tj|g¡}d}t || |jt|ƒt|ƒ¡|¡|S)zValidate type when a left hand annotation is present and we also
    could infer the right hand side::
 
        attrname: SomeType = Column(SomeDBType)
 
    NzRLeft hand assignment '{}: {}' not compatible with ORM mapped expression of type {})    rr6rÚNAMED_TYPE_SQLA_MAPPEDrr9r:r;r)rr!r"r.rGrHZeffective_typerAr/r/r0r=Ìs*
ÿÿýù
r=r)rr!r"r.r$cCsl|}|}|jr,t|jdƒ}t|jdƒ}n|}|}t|ttfƒsFt‚t|ttfƒsXt‚t||||||dS)Nr)rGrH)r5rr2rrr4r=)rr!r"r.rGrHZ left_hand_argZpython_type_argr/r/r0r<ös"úr<)rr!r"r$cCs@|dkr8d}t || |j¡|¡| tjttj    ƒg¡S|SdS)z‘Determine the type based on explicit annotation only.
 
    if no annotation were present, note that we need one there to know
    the type.
 
    Nz”Can't infer type from ORM mapped expression assigned to attribute '{}'; please specify a Python type or Mapped[<python type>] on the left hand side.)
rr9r:r;r6rrIrrZ special_form)rr!r"rAr/r/r0r+s ÿ
ÿr+rzSequence[Expression])rr!rFr$cCs¼|jdkrd|rd|d}t|tƒrVt|jtƒrV|jjD]}|jdkr4t|jgƒSq4n| tj    g¡S| 
d¡szt d|ƒ‚|  d¡}|dk    r˜t|jtƒsœt ‚t t|gƒ|jƒ}t|jdƒS)Nzsqlalchemy.sql.sqltypes.Enumrz    enum.Enumz"sqlalchemy.sql.type_api.TypeEnginez+could not extract Python type from node: %séÿÿÿÿ)Úfullnamer2rr!rrDrr6rZNAMED_TYPE_BUILTINS_STRZhas_baser4Zlookup_fully_qualified_or_nonerrr5)rr!rFZ    first_argZbase_Ztype_engine_symZ type_enginer/r/r0rE0s2 ÿ 
 ÿÿ
ÿþrE)N)NN)/Ú
__future__rÚtypingrrZ mypy.maptyperZ mypy.messagesrZ
mypy.nodesrrr    r
r r r rrrrZ mypy.pluginrZ mypy.subtypesrZ
mypy.typesrrrrrrrrÚrrr1r)r,r&r*r(r=r<r+rEr/r/r/r0Ú<module>sR                            ('  6ûiú*