-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-safe-datetime.test
More file actions
276 lines (238 loc) · 10.6 KB
/
check-safe-datetime.test
File metadata and controls
276 lines (238 loc) · 10.6 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
[case testDatetimeVsDateComparison]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
dt: datetime
d: date
if dt < d: # E: Unsupported operand types for < ("datetime" and "date")
pass
if d > dt: # E: Unsupported operand types for < ("datetime" and "date")
pass
if dt == d:
pass
if dt != d:
pass
if dt <= d: # E: Unsupported operand types for <= ("datetime" and "date")
pass
if dt >= d: # E: Unsupported operand types for >= ("datetime" and "date")
pass
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testDatetimeVsDateComparisonDisabled]
# No flags, so the error should not appear
from datetime import date, datetime
dt: datetime
d: date
if dt < d:
pass
if d > dt:
pass
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testDatetimeVsDateComparisonExplicitTypes]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
def compare_datetime_date(dt: datetime, d: date) -> bool:
return dt < d # E: Unsupported operand types for < ("datetime" and "date")
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testDatetimeComparisonOK]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
dt1: datetime
dt2: datetime
d1: date
d2: date
# datetime vs datetime is safe
if dt1 < dt2:
pass
# date vs date is now OK since inheritance blocking prevents datetime from being passed as date
if d1 < d2:
pass
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testDatetimeVsDateComparisonWithNow]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
if datetime.now() < date.today(): # E: Unsupported operand types for < ("datetime" and "date")
pass
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testDatetimeVsDateComparisonInExpression]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
dt: datetime
d: date
result = dt < d # E: Unsupported operand types for < ("datetime" and "date")
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testDateVsDateMixedWithDatetime]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
def compare_dates(d1: date, d2: date) -> bool:
# With inheritance blocking, this is now safe - datetime cannot be passed here
return d1 < d2
# Example usage that would fail at runtime:
dt = datetime.now()
d = date.today()
# This now errors because datetime is not assignable to date
result = compare_dates(dt, d) # E: Argument 1 to "compare_dates" has incompatible type "datetime"; expected "date"
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]
[case testInheritanceBlocking]
# flags: --enable-error-code unsafe-datetime
from datetime import date, datetime
# Assignment should be blocked
d: date = datetime.now() # E: Incompatible types in assignment (expression has type "datetime", variable has type "date")
# Function parameters should be blocked
def accept_date(d: date) -> None:
pass
accept_date(datetime.now()) # E: Argument 1 to "accept_date" has incompatible type "datetime"; expected "date"
# But date to date should still work
d2: date = date.today() # OK
accept_date(date.today()) # OK
[builtins fixtures/classmethod.pyi]
[file datetime.pyi]
class date:
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def today(cls) -> date: ...
def __lt__(self, other: date) -> bool: ...
def __le__(self, other: date) -> bool: ...
def __gt__(self, other: date) -> bool: ...
def __ge__(self, other: date) -> bool: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> None: ...
@classmethod
def now(cls) -> datetime: ...
def __lt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]