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
|
-- Test cases for invoking mypyc on the command line
--
-- These are slow -- do not add test cases unless you have a very good reason to do so.
[case testCompileMypyc]
# cmd: a.py b.py p/__init__.py p/q.py
import os.path
import p
import p.q
import a
import b
print('<main>', b.g(a.A()))
try:
a.f('')
except TypeError:
pass
else:
assert False
for x in [a, b, p, p.q]:
assert os.path.splitext(x.__file__)[1] != '.py'
[file z.py]
[file a.py]
import b
import c
from p import s
from typing import NamedTuple
print('<a>', ord('A') == 65) # Test full builtins
class A:
def __init__(self) -> None:
self.x = 4
def f(x: int) -> b.B:
return b.B(x)
class B:
def __init__(self, x: int, y: str) -> None:
self.x = x
print('<a>', f(5).x)
print('<c>', c.foo())
assert s.bar(10) == 20
class NT(NamedTuple):
x: int
print(NT(2))
[file b.py]
import a
import p.q
class B:
def __init__(self, x: int) -> None:
self.x = x
def g(z: 'a.A') -> int:
return p.q.foo(z.x)
print('<b>', 'here')
[file c.py]
def foo() -> int:
return 10
[file p/__init__.py]
[file p/q.py]
import p.r
def foo(x: int) -> int:
return x*p.r.foo(x)
[file p/r.py]
def foo(x: int) -> int:
return x
[file p/s.py]
def bar(x: int) -> int:
return x*2
[out]
<b> here
<a> True
<a> 5
<c> 10
NT(x=2)
<main> 16
-- This test is here so we can turn it on when we get nervous about
-- this case, but is disabled for speed reasons.
[case testCompileMypycOne-skip]
# cmd: a.py
import os.path
import a
assert os.path.splitext(a.__file__)[1] != '.py'
assert a.f(10) == 100
[file a.py]
def f(x: int) -> int:
return x*x
[case testErrorOutput1]
# cmd: test.py
[file test.py]
from functools import singledispatch
from mypy_extensions import trait
from typing import Any
def decorator(x: Any) -> Any:
return x
class NeverMetaclass(type): # E: Inheriting from most builtin types is unimplemented \
# N: Potential workaround: @mypy_extensions.mypyc_attr(native_class=False) \
# N: https://mypyc.readthedocs.io/en/stable/native_classes.html#defining-non-native-classes
pass
class Concrete1:
pass
@trait
class Trait1:
pass
class Concrete2:
pass
@decorator
class NonExt(Concrete1): # E: Non-extension classes may not inherit from extension classes
pass
class NopeMultipleInheritanceAndBadOrder3(Trait1, Concrete1, Concrete2): # E: Non-trait base must appear first in parent list
pass
class NopeBadOrder(Trait1, Concrete2): # E: Non-trait base must appear first in parent list
pass
class Foo:
pass
@singledispatch
def a(arg) -> None:
pass
@decorator # E: Calling decorator after registering function not supported
@a.register
def g(arg: int) -> None:
pass
@a.register
@decorator
def h(arg: str) -> None:
pass
@decorator
@decorator # E: Calling decorator after registering function not supported
@a.register
def i(arg: Foo) -> None:
pass
[case testErrorOutput2]
# cmd: test.py
[file test.py]
from typing import Final, List, Any, AsyncIterable
from mypy_extensions import trait, mypyc_attr
def busted(b: bool) -> None:
for i in range(1, 10, 0): # E: range() step can't be zero
try:
if i == 5:
break # E: break inside try/finally block is unimplemented
elif i == 4:
continue # E: continue inside try/finally block is unimplemented
finally:
print('oops')
print(sum([1,2,3]))
x = [1,2]
class Foo:
a, b = (10, 20) # E: Only assignment to variables is supported in class bodies
x[0] = 10 # E: Only assignment to variables is supported in class bodies
lol = 20
l = [10] # W: Unsupported default attribute value
c = d = 50 # E: Multiple assignment in class bodies not supported
if 1+1 == 2: # E: Unsupported statement in class body
x = 10
Foo.lol = 50 # E: Only class variables defined as ClassVar can be assigned to
def decorator(x: Any) -> Any:
return x
class Concrete1:
pass
@trait
class PureTrait:
pass
@trait
class Trait1:
pass
class Concrete2:
pass
@trait
class Trait2(Concrete2):
pass
class NopeMultipleInheritance(Concrete1, Concrete2): # E: Multiple inheritance is not supported (except for traits)
pass
class NopeMultipleInheritanceAndBadOrder(Concrete1, Trait1, Concrete2): # E: Multiple inheritance is not supported (except for traits)
pass
class NopeMultipleInheritanceAndBadOrder2(Concrete1, Concrete2, Trait1): # E: Multiple inheritance is not supported (except for traits)
pass
@decorator
class NonExt2:
@property # E: Property setters not supported in non-extension classes
def test(self) -> int:
return 0
@test.setter
def test(self, x: int) -> None:
pass
iterator_warning = (i+1 for i in range(10)) # W: Treating generator comprehension as list
# But we don't want warnings for these cases:
tup = tuple(i+1 for i in range(10))
a_str = " ".join(str(i) for i in range(10))
wtvr = next(i for i in range(10) if i == 5)
d1 = {1: 2}
# Since PR 18180, the following pattern should pose no problems anymore:
def f(l: List[object]) -> None:
x = None
for i in l:
if x is None:
x = i
@mypyc_attr(allow_interpreted_subclasses=True)
class AllowInterp1(Concrete1): # E: Base class "test.Concrete1" does not allow interpreted subclasses
pass
@mypyc_attr(allow_interpreted_subclasses=True)
class AllowInterp2(PureTrait): # E: Base class "test.PureTrait" does not allow interpreted subclasses
pass
async def async_generators() -> AsyncIterable[int]:
yield 1 # E: async generators are unimplemented
[case testOnlyWarningOutput]
# cmd: test.py
[file test.py]
names = (str(v) for v in [1, 2, 3]) # W: Treating generator comprehension as list
[case testSubPackage]
# cmd: pkg/sub/foo.py
from pkg.sub import foo
[file pkg/__init__.py]
[file pkg/sub/__init__.py]
print("importing...")
from . import foo
print("done")
[file pkg/sub/foo.py]
print("imported foo")
[out]
importing...
imported foo
done
[case testImportFromInitPy]
# cmd: foo.py
import foo
[file pkg2/__init__.py]
[file pkg2/mod2.py]
class A:
class B:
pass
[file pkg1/__init__.py]
from pkg2.mod2 import A
[file foo.py]
import pkg1
from typing import TypedDict
class Eggs(TypedDict):
obj1: pkg1.A.B
print(type(Eggs(obj1=pkg1.A.B())["obj1"]).__name__)
print(type(Eggs(obj1=pkg1.A.B())["obj1"]).__module__)
[out]
B
pkg2.mod2
|