Skip to content

Commit 395108d

Browse files
Newbyteepre-commit-ci[bot]hauntsaninja
authored
[mypyc] Support ellipsis (...) expressions in class bodies (#17923)
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 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: hauntsaninja <[email protected]>
1 parent 1a074b6 commit 395108d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

mypyc/irbuild/classdef.py

Lines changed: 4 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,9 @@ 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 isinstance(stmt, PassStmt) or (
150+
isinstance(stmt, ExpressionStmt) and isinstance(stmt.expr, EllipsisExpr)
151+
):
149152
continue
150153
elif isinstance(stmt, AssignmentStmt):
151154
if len(stmt.lvalues) != 1:

mypyc/test-data/run-classes.test

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,36 @@ class Empty: pass
33

44
def f(e: Empty) -> Empty:
55
return e
6+
7+
class EmptyEllipsis: ...
8+
9+
def g(e: EmptyEllipsis) -> EmptyEllipsis:
10+
return e
611
[file driver.py]
7-
from native import Empty, f
12+
from native import Empty, EmptyEllipsis, f, g
813

914
print(isinstance(Empty, type))
1015
print(Empty)
1116
print(str(Empty())[:20])
1217

1318
e = Empty()
1419
print(f(e) is e)
20+
21+
print(isinstance(EmptyEllipsis, type))
22+
print(EmptyEllipsis)
23+
print(str(EmptyEllipsis())[:28])
24+
25+
e2 = EmptyEllipsis()
26+
print(g(e2) is e2)
1527
[out]
1628
True
1729
<class 'native.Empty'>
1830
<native.Empty object
1931
True
32+
True
33+
<class 'native.EmptyEllipsis'>
34+
<native.EmptyEllipsis object
35+
True
2036

2137
[case testClassWithFields]
2238
class C:

0 commit comments

Comments
 (0)