| 1 |
-- Test cases for invoking mypyc on the command line |
| 2 |
-- |
| 3 |
-- These are slow -- do not add test cases unless you have a very good reason to do so. |
| 4 |
|
| 5 |
[case testCompileMypyc] |
| 6 |
# cmd: a.py b.py p/__init__.py p/q.py |
| 7 |
import os.path |
| 8 |
import p |
| 9 |
import p.q |
| 10 |
import a |
| 11 |
import b |
| 12 |
print('<main>', b.g(a.A())) |
| 13 |
try: |
| 14 |
a.f('') |
| 15 |
except TypeError: |
| 16 |
pass |
| 17 |
else: |
| 18 |
assert False |
| 19 |
for x in [a, b, p, p.q]: |
| 20 |
assert os.path.splitext(x.__file__)[1] != '.py' |
| 21 |
[file z.py] |
| 22 |
|
| 23 |
[file a.py] |
| 24 |
import b |
| 25 |
import c |
| 26 |
from p import s |
| 27 |
from typing import NamedTuple |
| 28 |
|
| 29 |
print('<a>', ord('A') == 65) # Test full builtins |
| 30 |
|
| 31 |
class A: |
| 32 |
def __init__(self) -> None: |
| 33 |
self.x = 4 |
| 34 |
|
| 35 |
def f(x: int) -> b.B: |
| 36 |
return b.B(x) |
| 37 |
|
| 38 |
class B: |
| 39 |
def __init__(self, x: int, y: str) -> None: |
| 40 |
self.x = x |
| 41 |
|
| 42 |
print('<a>', f(5).x) |
| 43 |
print('<c>', c.foo()) |
| 44 |
assert s.bar(10) == 20 |
| 45 |
|
| 46 |
class NT(NamedTuple): |
| 47 |
x: int |
| 48 |
|
| 49 |
print(NT(2)) |
| 50 |
|
| 51 |
[file b.py] |
| 52 |
import a |
| 53 |
import p.q |
| 54 |
|
| 55 |
class B: |
| 56 |
def __init__(self, x: int) -> None: |
| 57 |
self.x = x |
| 58 |
|
| 59 |
def g(z: 'a.A') -> int: |
| 60 |
return p.q.foo(z.x) |
| 61 |
|
| 62 |
print('<b>', 'here') |
| 63 |
|
| 64 |
[file c.py] |
| 65 |
def foo() -> int: |
| 66 |
return 10 |
| 67 |
|
| 68 |
[file p/__init__.py] |
| 69 |
|
| 70 |
[file p/q.py] |
| 71 |
import p.r |
| 72 |
def foo(x: int) -> int: |
| 73 |
return x*p.r.foo(x) |
| 74 |
|
| 75 |
[file p/r.py] |
| 76 |
def foo(x: int) -> int: |
| 77 |
return x |
| 78 |
|
| 79 |
[file p/s.py] |
| 80 |
def bar(x: int) -> int: |
| 81 |
return x*2 |
| 82 |
|
| 83 |
[out] |
| 84 |
<b> here |
| 85 |
<a> True |
| 86 |
<a> 5 |
| 87 |
<c> 10 |
| 88 |
NT(x=2) |
| 89 |
<main> 16 |
| 90 |
|
| 91 |
-- This test is here so we can turn it on when we get nervous about |
| 92 |
-- this case, but is disabled for speed reasons. |
| 93 |
[case testCompileMypycOne-skip] |
| 94 |
# cmd: a.py |
| 95 |
import os.path |
| 96 |
import a |
| 97 |
assert os.path.splitext(a.__file__)[1] != '.py' |
| 98 |
assert a.f(10) == 100 |
| 99 |
|
| 100 |
[file a.py] |
| 101 |
def f(x: int) -> int: |
| 102 |
return x*x |
| 103 |
|
| 104 |
[case testErrorOutput1] |
| 105 |
# cmd: test.py |
| 106 |
|
| 107 |
[file test.py] |
| 108 |
from functools import singledispatch |
| 109 |
from mypy_extensions import trait |
| 110 |
from typing import Any |
| 111 |
|
| 112 |
def decorator(x: Any) -> Any: |
| 113 |
return x |
| 114 |
|
| 115 |
class NeverMetaclass(type): # E: Inheriting from most builtin types is unimplemented \ |
| 116 |
# N: Potential workaround: @mypy_extensions.mypyc_attr(native_class=False) \ |
| 117 |
# N: https://mypyc.readthedocs.io/en/stable/native_classes.html#defining-non-native-classes |
| 118 |
pass |
| 119 |
|
| 120 |
class Concrete1: |
| 121 |
pass |
| 122 |
|
| 123 |
@trait |
| 124 |
class Trait1: |
| 125 |
pass |
| 126 |
|
| 127 |
class Concrete2: |
| 128 |
pass |
| 129 |
|
| 130 |
@decorator |
| 131 |
class NonExt(Concrete1): # E: Non-extension classes may not inherit from extension classes |
| 132 |
pass |
| 133 |
|
| 134 |
class NopeMultipleInheritanceAndBadOrder3(Trait1, Concrete1, Concrete2): # E: Non-trait base must appear first in parent list |
| 135 |
pass |
| 136 |
|
| 137 |
class NopeBadOrder(Trait1, Concrete2): # E: Non-trait base must appear first in parent list |
| 138 |
pass |
| 139 |
|
| 140 |
class Foo: |
| 141 |
pass |
| 142 |
|
| 143 |
@singledispatch |
| 144 |
def a(arg) -> None: |
| 145 |
pass |
| 146 |
|
| 147 |
@decorator # E: Calling decorator after registering function not supported |
| 148 |
@a.register |
| 149 |
def g(arg: int) -> None: |
| 150 |
pass |
| 151 |
|
| 152 |
@a.register |
| 153 |
@decorator |
| 154 |
def h(arg: str) -> None: |
| 155 |
pass |
| 156 |
|
| 157 |
@decorator |
| 158 |
@decorator # E: Calling decorator after registering function not supported |
| 159 |
@a.register |
| 160 |
def i(arg: Foo) -> None: |
| 161 |
pass |
| 162 |
|
| 163 |
[case testErrorOutput2] |
| 164 |
# cmd: test.py |
| 165 |
|
| 166 |
[file test.py] |
| 167 |
from typing import Final, List, Any, AsyncIterable |
| 168 |
from mypy_extensions import trait, mypyc_attr |
| 169 |
|
| 170 |
def busted(b: bool) -> None: |
| 171 |
for i in range(1, 10, 0): # E: range() step can't be zero |
| 172 |
try: |
| 173 |
if i == 5: |
| 174 |
break # E: break inside try/finally block is unimplemented |
| 175 |
elif i == 4: |
| 176 |
continue # E: continue inside try/finally block is unimplemented |
| 177 |
finally: |
| 178 |
print('oops') |
| 179 |
|
| 180 |
print(sum([1,2,3])) |
| 181 |
|
| 182 |
x = [1,2] |
| 183 |
|
| 184 |
class Foo: |
| 185 |
a, b = (10, 20) # E: Only assignment to variables is supported in class bodies |
| 186 |
x[0] = 10 # E: Only assignment to variables is supported in class bodies |
| 187 |
lol = 20 |
| 188 |
l = [10] # W: Unsupported default attribute value |
| 189 |
c = d = 50 # E: Multiple assignment in class bodies not supported |
| 190 |
|
| 191 |
if 1+1 == 2: # E: Unsupported statement in class body |
| 192 |
x = 10 |
| 193 |
|
| 194 |
Foo.lol = 50 # E: Only class variables defined as ClassVar can be assigned to |
| 195 |
|
| 196 |
def decorator(x: Any) -> Any: |
| 197 |
return x |
| 198 |
|
| 199 |
class Concrete1: |
| 200 |
pass |
| 201 |
|
| 202 |
@trait |
| 203 |
class PureTrait: |
| 204 |
pass |
| 205 |
|
| 206 |
@trait |
| 207 |
class Trait1: |
| 208 |
pass |
| 209 |
|
| 210 |
class Concrete2: |
| 211 |
pass |
| 212 |
|
| 213 |
@trait |
| 214 |
class Trait2(Concrete2): |
| 215 |
pass |
| 216 |
|
| 217 |
class NopeMultipleInheritance(Concrete1, Concrete2): # E: Multiple inheritance is not supported (except for traits) |
| 218 |
pass |
| 219 |
|
| 220 |
class NopeMultipleInheritanceAndBadOrder(Concrete1, Trait1, Concrete2): # E: Multiple inheritance is not supported (except for traits) |
| 221 |
pass |
| 222 |
|
| 223 |
class NopeMultipleInheritanceAndBadOrder2(Concrete1, Concrete2, Trait1): # E: Multiple inheritance is not supported (except for traits) |
| 224 |
pass |
| 225 |
|
| 226 |
@decorator |
| 227 |
class NonExt2: |
| 228 |
@property # E: Property setters not supported in non-extension classes |
| 229 |
def test(self) -> int: |
| 230 |
return 0 |
| 231 |
|
| 232 |
@test.setter |
| 233 |
def test(self, x: int) -> None: |
| 234 |
pass |
| 235 |
|
| 236 |
iterator_warning = (i+1 for i in range(10)) # W: Treating generator comprehension as list |
| 237 |
|
| 238 |
# But we don't want warnings for these cases: |
| 239 |
tup = tuple(i+1 for i in range(10)) |
| 240 |
a_str = " ".join(str(i) for i in range(10)) |
| 241 |
wtvr = next(i for i in range(10) if i == 5) |
| 242 |
|
| 243 |
d1 = {1: 2} |
| 244 |
|
| 245 |
# Since PR 18180, the following pattern should pose no problems anymore: |
| 246 |
def f(l: List[object]) -> None: |
| 247 |
x = None |
| 248 |
for i in l: |
| 249 |
if x is None: |
| 250 |
x = i |
| 251 |
|
| 252 |
@mypyc_attr(allow_interpreted_subclasses=True) |
| 253 |
class AllowInterp1(Concrete1): # E: Base class "test.Concrete1" does not allow interpreted subclasses |
| 254 |
pass |
| 255 |
|
| 256 |
@mypyc_attr(allow_interpreted_subclasses=True) |
| 257 |
class AllowInterp2(PureTrait): # E: Base class "test.PureTrait" does not allow interpreted subclasses |
| 258 |
pass |
| 259 |
|
| 260 |
async def async_generators() -> AsyncIterable[int]: |
| 261 |
yield 1 # E: async generators are unimplemented |
| 262 |
|
| 263 |
[case testOnlyWarningOutput] |
| 264 |
# cmd: test.py |
| 265 |
|
| 266 |
[file test.py] |
| 267 |
names = (str(v) for v in [1, 2, 3]) # W: Treating generator comprehension as list |
| 268 |
|
| 269 |
[case testSubPackage] |
| 270 |
# cmd: pkg/sub/foo.py |
| 271 |
from pkg.sub import foo |
| 272 |
|
| 273 |
[file pkg/__init__.py] |
| 274 |
|
| 275 |
[file pkg/sub/__init__.py] |
| 276 |
print("importing...") |
| 277 |
from . import foo |
| 278 |
print("done") |
| 279 |
|
| 280 |
[file pkg/sub/foo.py] |
| 281 |
print("imported foo") |
| 282 |
|
| 283 |
[out] |
| 284 |
importing... |
| 285 |
imported foo |
| 286 |
done |
| 287 |
|
| 288 |
[case testImportFromInitPy] |
| 289 |
# cmd: foo.py |
| 290 |
import foo |
| 291 |
|
| 292 |
[file pkg2/__init__.py] |
| 293 |
|
| 294 |
[file pkg2/mod2.py] |
| 295 |
class A: |
| 296 |
class B: |
| 297 |
pass |
| 298 |
|
| 299 |
[file pkg1/__init__.py] |
| 300 |
from pkg2.mod2 import A |
| 301 |
|
| 302 |
[file foo.py] |
| 303 |
import pkg1 |
| 304 |
from typing import TypedDict |
| 305 |
|
| 306 |
class Eggs(TypedDict): |
| 307 |
obj1: pkg1.A.B |
| 308 |
|
| 309 |
print(type(Eggs(obj1=pkg1.A.B())["obj1"]).__name__) |
| 310 |
print(type(Eggs(obj1=pkg1.A.B())["obj1"]).__module__) |
| 311 |
|
| 312 |
[out] |
| 313 |
B |
| 314 |
pkg2.mod2 |
| 315 |
|
| 316 |
[case testCompilePackageOnlyInitPy] |
| 317 |
# cmd: pkg/__init__.py |
| 318 |
import os.path |
| 319 |
import pkg |
| 320 |
|
| 321 |
print(pkg.x) |
| 322 |
assert os.path.splitext(pkg.__file__)[1] != ".py" |
| 323 |
|
| 324 |
[file pkg/__init__.py] |
| 325 |
x: int = 1 |
| 326 |
|
| 327 |
[out] |
| 328 |
1 |
| 329 |
|
| 330 |
[case testStrictBytesRequired] |
| 331 |
# cmd: --no-strict-bytes a.py |
| 332 |
|
| 333 |
[file a.py] |
| 334 |
def f(b: bytes) -> None: pass |
| 335 |
f(bytearray()) |
| 336 |
|
| 337 |
[out] |
| 338 |
a.py: error: Option --strict-bytes cannot be disabled when using mypyc |