Skip to content

Commit 48c7990

Browse files
committed
[mypyc] Support ellipsis (...) expressions in class bodies
This can be used to declare concise custom exceptions, e.g. class UnknownReleaseError(ValueError): ... which otherwise probably would be written class UnknownReleaseError(ValueError): pass and is supported by CPython. Closes mypyc/mypyc#1069
1 parent 46c108e commit 48c7990

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

mypyc/irbuild/classdef.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
CallExpr,
1313
ClassDef,
1414
Decorator,
15+
EllipsisExpr,
1516
ExpressionStmt,
1617
FuncDef,
1718
Lvalue,
@@ -145,7 +146,11 @@ def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None:
145146
continue
146147
with builder.catch_errors(stmt.line):
147148
cls_builder.add_method(get_func_def(stmt))
148-
elif isinstance(stmt, PassStmt):
149+
elif (
150+
isinstance(stmt, PassStmt)
151+
or (isinstance(stmt, ExpressionStmt)
152+
and isinstance(stmt.expr, EllipsisExpr))
153+
):
149154
continue
150155
elif isinstance(stmt, AssignmentStmt):
151156
if len(stmt.lvalues) != 1:

mypyc/test-data/run-classes.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ True
1818
<native.Empty object
1919
True
2020

21+
[case testEmptyClassEllipsis]
22+
class EmptyEllipsis: ...
23+
24+
def f(e: EmptyEllipsis) -> EmptyEllipsis:
25+
return e
26+
[file driver.py]
27+
from native import EmptyEllipsis, f
28+
29+
print(isinstance(EmptyEllipsis, type))
30+
print(EmptyEllipsis)
31+
print(str(EmptyEllipsis())[:20])
32+
33+
e = EmptyEllipsis()
34+
print(f(e) is e)
35+
[out]
36+
True
37+
<class 'native.EmptyEllipsis'>
38+
<native.EmptyEllipsis object
39+
True
40+
2141
[case testClassWithFields]
2242
class C:
2343
x: int

0 commit comments

Comments
 (0)