From d6bd319db431e591627ca70c0ac62883c13e6aaa Mon Sep 17 00:00:00 2001 From: Stefan Hansson Date: Fri, 11 Oct 2024 12:14:35 +0200 Subject: [PATCH 1/3] [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 https://github.com/mypyc/mypyc/issues/1069 --- mypyc/irbuild/classdef.py | 7 ++++++- mypyc/test-data/run-classes.test | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 7e0a842b1b41..17ce7ea34776 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -12,6 +12,7 @@ CallExpr, ClassDef, Decorator, + EllipsisExpr, ExpressionStmt, FuncDef, Lvalue, @@ -145,7 +146,11 @@ def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None: continue with builder.catch_errors(stmt.line): cls_builder.add_method(get_func_def(stmt)) - elif isinstance(stmt, PassStmt): + elif ( + isinstance(stmt, PassStmt) + or (isinstance(stmt, ExpressionStmt) + and isinstance(stmt.expr, EllipsisExpr)) + ): continue elif isinstance(stmt, AssignmentStmt): if len(stmt.lvalues) != 1: diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index 59617714f7e7..9b1a157439a3 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -18,6 +18,26 @@ True Empty: + return e +[file driver.py] +from native import Empty, f + +print(isinstance(Empty, type)) +print(Empty) +print(str(Empty())[:20]) + +e = Empty() +print(f(e) is e) +[out] +True + + Date: Fri, 11 Oct 2024 19:25:44 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypyc/irbuild/classdef.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 17ce7ea34776..bcc9594adcb9 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -146,10 +146,8 @@ def transform_class_def(builder: IRBuilder, cdef: ClassDef) -> None: continue with builder.catch_errors(stmt.line): cls_builder.add_method(get_func_def(stmt)) - elif ( - isinstance(stmt, PassStmt) - or (isinstance(stmt, ExpressionStmt) - and isinstance(stmt.expr, EllipsisExpr)) + elif isinstance(stmt, PassStmt) or ( + isinstance(stmt, ExpressionStmt) and isinstance(stmt.expr, EllipsisExpr) ): continue elif isinstance(stmt, AssignmentStmt): From ab32f6e5c07307d12a9de24c92e66e8e369c81b0 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 13 Oct 2024 18:45:30 -0700 Subject: [PATCH 3/3] combine test cases --- mypyc/test-data/run-classes.test | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index 9b1a157439a3..7c2998874f78 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -3,28 +3,13 @@ class Empty: pass def f(e: Empty) -> Empty: return e -[file driver.py] -from native import Empty, f - -print(isinstance(Empty, type)) -print(Empty) -print(str(Empty())[:20]) - -e = Empty() -print(f(e) is e) -[out] -True - - Empty: +def g(e: EmptyEllipsis) -> EmptyEllipsis: return e [file driver.py] -from native import Empty, f +from native import Empty, EmptyEllipsis, f, g print(isinstance(Empty, type)) print(Empty) @@ -32,11 +17,22 @@ print(str(Empty())[:20]) e = Empty() print(f(e) is e) + +print(isinstance(EmptyEllipsis, type)) +print(EmptyEllipsis) +print(str(EmptyEllipsis())[:28]) + +e2 = EmptyEllipsis() +print(g(e2) is e2) [out] True +