Skip to content

Commit 5b3d4bc

Browse files
committed
Account for complex args to Complex
1 parent 5c9a514 commit 5b3d4bc

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

NEWS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Bug Fixes
1616
`(except [] …)`, which catches all exceptions.
1717
* `(except [e []] …)` is now translated to Python correctly by `hy2py`.
1818
* Fixed a bug where logical ops starting with a `(setv …)` expression failed to compile.
19+
* A complex `imag` argument to `hy.models.Complex` is no longer allowed.
1920

2021
1.0.0 ("Afternoon Review", released 2024-09-22)
2122
======================================================================

hy/models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ def __new__(cls, num, *args, **kwargs):
343343

344344
class Complex(Object, complex):
345345
"""
346-
Represents a literal floating-point complex number (:class:`complex`).
346+
Represents a literal floating-point complex number (:class:`complex`). If
347+
``real`` is itself a :class:`complex` object, its imaginary part is extracted and
348+
added to the imaginary part of the new model, but ``imag``, if provided, must be
349+
real.
347350
"""
348351

349352
def __new__(cls, real, imag=0, *args, **kwargs):
@@ -354,6 +357,12 @@ def __new__(cls, real, imag=0, *args, **kwargs):
354357
if p2:
355358
check_inf_nan_cap(p2, value.imag)
356359
return value
360+
if isinstance(imag, complex):
361+
raise TypeError("`imag` must be real")
362+
if isinstance(real, complex):
363+
# This is deprecated by Python 3.14's `complex`, so
364+
# extract the imaginary part before passing through.
365+
real, imag = real.real, imag + real.imag
357366
return super().__new__(cls, real, imag)
358367

359368

0 commit comments

Comments
 (0)