-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathsqlalchemy-plugin-features.test
More file actions
372 lines (285 loc) · 10.5 KB
/
sqlalchemy-plugin-features.test
File metadata and controls
372 lines (285 loc) · 10.5 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
[case testModelInitColumnDeclared]
# flags: --strict-optional
from sqlalchemy import Column, Integer, String
from base import Base
from typing import Optional
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String())
oi: Optional[int]
os: Optional[str]
User()
User(1, 2) # E: Too many arguments for "User"
User(id=int(), name=str())
User(id=oi) # E: Incompatible type for "id" of "User" (got "Optional[int]", expected "int")
User(name=os)
[file base.py]
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
[out]
[case testModelInitColumnDecorated]
# flags: --strict-optional
from sqlalchemy import Column, Integer, String
from base import Base
from typing import Optional
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String())
oi: Optional[int]
os: Optional[str]
User()
User(1, 2) # E: Too many arguments for "User"
User(id=int(), name=str())
User(id=oi) # E: Incompatible type for "id" of "User" (got "Optional[int]", expected "int")
User(name=os)
[file base.py]
from sqlalchemy.ext.declarative import as_declarative
@as_declarative()
class Base:
...
[out]
[case testModelInitRelationship]
from typing import TYPE_CHECKING, List
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from base import Base
if TYPE_CHECKING:
from other import Other
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
other = relationship('Other', uselist=False)
many_others = relationship(Other, uselist=True)
o: Other
mo: List[Other]
User()
User(other=o, many_others=mo)
User(other=mo) # E: Incompatible type for "other" of "User" (got "List[Other]", expected "Other")
User(unknown=42) # E: Unexpected column "unknown" for model "User"
[file other.py]
from sqlalchemy import Column, Integer, String
from base import Base
class Other(Base):
__tablename__ = 'other'
id = Column(Integer(), primary_key=True)
name = Column(String(), nullable=False)
[file base.py]
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
[out]
[case testRelationshipType]
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from typing import Iterable
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
first_other = relationship(Other)
second_other = relationship(Other, uselist=True)
bad_other: RelationshipProperty[int] = relationship(Other, uselist=False) # E: Incompatible types in assignment (expression has type "RelationshipProperty[Other]", variable has type "RelationshipProperty[int]")
user = User()
reveal_type(user.first_other) # E: Revealed type is 'main.Other*'
reveal_type(user.second_other) # E: Revealed type is 'typing.Iterable*[main.Other]'
class Other(Base):
__tablename__ = 'other'
id = Column(Integer(), primary_key=True)
[out]
[case testRelationshipString]
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from typing import Iterable
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
first_other = relationship('Other', uselist=False)
second_other = relationship('Other', uselist=True)
second_bad_other = relationship('What') # E: Cannot find model "What" \
# N: Only imported models can be found; use "if TYPE_CHECKING: ..." to avoid import cycles
user = User()
reveal_type(user.first_other) # E: Revealed type is 'main.Other*'
reveal_type(user.second_other) # E: Revealed type is 'typing.Iterable*[main.Other]'
class Other(Base):
__tablename__ = 'other'
id = Column(Integer(), primary_key=True)
[out]
[case testRelationshipAnnotated]
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from typing import Iterable
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
first_other: RelationshipProperty[Other] = relationship('Other')
second_other: RelationshipProperty[Iterable[Other]] = relationship(Other, uselist=True)
third_other: RelationshipProperty[Other] = relationship(Other, uselist=False)
bad_other: RelationshipProperty[Other] = relationship('Other', uselist=True) # E: Incompatible types in assignment (expression has type "RelationshipProperty[Iterable[Other]]", variable has type "RelationshipProperty[Other]")
class Other(Base):
__tablename__ = 'other'
id = Column(Integer(), primary_key=True)
[out]
[case testColumnCombo]
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String(), default='John Doe', nullable=True)
user: User
reveal_type(User.name) # E: Revealed type is 'sqlalchemy.sql.schema.Column[Union[builtins.str*, None]]'
[out]
[case testAddedAttributesDeclared]
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String(), default='John Doe', nullable=True)
user: User
reveal_type(User.metadata) # E: Revealed type is 'sqlalchemy.sql.schema.MetaData'
reveal_type(User.__table__) # E: Revealed type is 'sqlalchemy.sql.schema.Table'
[out]
[case testAddedAttributedDecorated]
from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy import Column, Integer, String
@as_declarative()
class Base:
...
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String(), default='John Doe', nullable=True)
user: User
reveal_type(User.metadata) # E: Revealed type is 'sqlalchemy.sql.schema.MetaData'
reveal_type(User.__table__) # E: Revealed type is 'sqlalchemy.sql.schema.Table'
[out]
[case testKwArgsModelOK]
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
record = {'name': 'John Doe'}
User(**record) # OK
[out]
[case testGrouping]
from sqlalchemy import Column, String, text
from sqlalchemy.sql.elements import Grouping
reveal_type(Grouping(Column(String, nullable=False))) # E: Revealed type is 'sqlalchemy.sql.elements.Grouping[builtins.str*]'
reveal_type(Grouping(text('foo'))) # E: Revealed type is 'sqlalchemy.sql.elements.Grouping[None]'
[case testDeclarativeBaseWithBaseClass]
from sqlalchemy import Column, Integer, String
from base import Base
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String())
user: User
reveal_type(user.f()) # E: Revealed type is 'builtins.str'
[file base.py]
from sqlalchemy.ext.declarative import declarative_base
class Model:
def f(self) -> str: ...
Base = declarative_base(cls=Model)
[out]
[case testDeclarativeBaseWithMultipleBaseClasses]
from sqlalchemy import Column, Integer, String
from base import Base
class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
name = Column(String())
user: User
reveal_type(user.f()) # E: Revealed type is 'builtins.str'
reveal_type(user.g()) # E: Revealed type is 'builtins.int'
[file base.py]
from sqlalchemy.ext.declarative import declarative_base
class Model:
def f(self) -> str: ...
class Model2:
def g(self) -> int: ...
Base = declarative_base(cls=(Model, Model2))
[out]
[case testDeclarativeBaseWithBaseClassWrongMRO]
from sqlalchemy.ext.declarative import declarative_base
class M1:
...
class M2(M1):
...
Base = declarative_base(cls=(M1, M2)) # E: Not able to calculate MRO for declarative base
reveal_type(Base) # E: Revealed type is 'Any'
[out]
[case testRelationshipIsGuessed]
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True)
name = Column(String)
children = relationship("Child")
class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True)
name = Column(String)
parent_id = Column(Integer, ForeignKey(Parent.id))
parent = relationship(Parent)
child: Child
parent: Parent
reveal_type(child.parent) # E: Revealed type is 'main.Parent*'
reveal_type(parent.children) # E: Revealed type is 'typing.Iterable*[main.Child]'
[out]
[case testRelationshipIsGuessed2]
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True)
name = Column(String)
children = relationship("Child")
class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True)
name = Column(String)
parent_id = Column(Integer, ForeignKey("parents.id"))
parent = relationship(Parent)
child: Child
parent: Parent
reveal_type(child.parent) # E: Revealed type is 'main.Parent*'
reveal_type(parent.children) # E: Revealed type is 'typing.Iterable*[main.Child]'
[out]
[case testRelationshipIsGuessed3]
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True)
name = Column(String)
children = relationship("Child")
class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True)
name = Column(String)
parent_id = Column(Integer, ForeignKey("other_parents.id"))
parent = relationship(Parent)
child: Child
parent: Parent
reveal_type(child.parent) # E: Revealed type is 'main.Parent*'
reveal_type(parent.children) # E: Revealed type is 'main.Child*'
[out]