Skip to content

Commit 5c8304b

Browse files
authored
Fix Python SyntaxWarnings generated by compiled code (#998)
Fixes #996
1 parent d0b46c9 commit 5c8304b

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
* Added `*default-data-reader-fn*` (#924)
1212
* Added `basilisp.pprint/print-table` function (#983)
1313
* Added `basilisp.core/read-all` function (#986)
14-
* Added various compiler arguments to cli commands (#989)
14+
* Added various compiler arguments to CLI commands (#989)
1515

1616
### Changed
17-
* Improved on the nREPL server exception messages by matching that of the REPL user friendly format (#968)
17+
* Improved on the nREPL server exception messages by matching that of the REPL user-friendly format (#968)
1818
* Types created via `deftype` and `reify` may declare supertypes as abstract (taking precedence over true `abc.ABC` types) and specify their member list using `^:abstract-members` metadata (#942)
19-
* Load functions (`load`, `load-file`, `load-reader`, etc) now return the value of the last form evaluated. (#984)
19+
* Load functions (`load`, `load-file`, `load-reader`, etc.) now return the value of the last form evaluated. (#984)
2020

2121
### Fixed
22-
* Fixed inconsistent behavior with `basilisp.core/with` when the `body` contains more than one form (#981)
23-
* Fixed an issue with `basilisp.core/time` failing when called outside of the core ns (#991)
24-
* Fixed an issue with `basilisp.core/promise` where a thread waiting for a value from another thread might not wake up immediately upon delivery (#983).
25-
* Fixed using keyword as a function not returning the default value in some cases (#997)
22+
* Fix inconsistent behavior with `basilisp.core/with` when the `body` contains more than one form (#981)
23+
* Fix an issue with `basilisp.core/time` failing when called outside `basilisp.core` (#991)
24+
* Fix an issue with `basilisp.core/promise` where a thread waiting for a value from another thread might not wake up immediately upon delivery (#983).
25+
* Fix using keyword as a function not returning the default value in some cases (#997)
26+
* Fix an issue where Python `SyntaxWarning`s would be emitted for certain compiled code (#???)
2627

2728
### Removed
2829
* Removed `python-dateutil` and `readerwriterlock` as dependencies, switching to standard library components instead (#976)

src/basilisp/core.lpy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@
16061606
[x]
16071607
(python/int x))
16081608

1609-
(defn ^:inline boolean
1609+
(defn ^:no-inline boolean
16101610
"Coerce ``x`` to a boolean.
16111611

16121612
Only ``false`` and ``nil`` are logical false and will return ``false``\\. All

src/basilisp/lang/compiler/optimizer.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ def _filter_dead_code(nodes: Iterable[ast.stmt]) -> List[ast.stmt]:
2020
return new_nodes
2121

2222

23+
def _needs_eq_operator(arg: ast.expr) -> bool:
24+
return isinstance(arg, ast.Constant) and all(
25+
arg.value is not v for v in (True, False, None, ...)
26+
)
27+
28+
2329
@functools.singledispatch
2430
def _optimize_operator_call( # pylint: disable=unused-argument
2531
fn: ast.AST, node: ast.Call
@@ -73,14 +79,25 @@ def _optimize_operator_call_attr( # pylint: disable=too-many-return-statements
7379
"ne": ast.NotEq,
7480
"gt": ast.Gt,
7581
"ge": ast.GtE,
76-
"is_": ast.Is,
77-
"is_not": ast.IsNot,
7882
}.get(fn.attr)
7983
if compareop is not None:
8084
arg1, arg2 = node.args
8185
assert len(node.args) == 2
8286
return ast.Compare(arg1, [compareop()], [arg2])
8387

88+
isop = {
89+
"is_": (ast.Is, ast.Eq),
90+
"is_not": (ast.IsNot, ast.NotEq),
91+
}.get(fn.attr)
92+
if isop is not None:
93+
isoper, eqoper = isop
94+
arg1, arg2 = node.args
95+
assert len(node.args) == 2
96+
oper = (
97+
eqoper if any(_needs_eq_operator(arg) for arg in node.args) else isoper
98+
)
99+
return ast.Compare(arg1, [oper()], [arg2])
100+
84101
if fn.attr == "contains":
85102
arg1, arg2 = node.args
86103
assert len(node.args) == 2

src/basilisp/lang/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
_DOUBLE_DOT_REPLACEMENT = "__DOT_DOT__"
1515

1616
_MUNGE_REPLACEMENTS = {
17+
"'": "__PRIME__",
1718
"+": "__PLUS__",
1819
"-": "_",
1920
"*": "__STAR__",

tests/basilisp/core_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ def test_ex_message_standard_exception(self):
10831083
try:
10841084
raise Exception("Exception Message")
10851085
except Exception as e:
1086-
assert "Exception Message" is core.ex_message(e)
1086+
assert "Exception Message" == core.ex_message(e)
10871087

10881088

10891089
class TestBitManipulation:

tests/basilisp/keyword_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_keyword_as_function():
6262

6363
assert kw == kw(lset.s(kw))
6464
assert None is kw(lset.s(1))
65-
assert "hi" is kw(lset.s(1), default="hi")
65+
assert "hi" == kw(lset.s(1), default="hi")
6666

6767
assert 1 is kw(None, 1)
6868
assert None is kw(None, None)

tests/basilisp/symbol_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_symbol_as_function():
5656

5757
assert sym == sym(lset.s(sym))
5858
assert None is sym(lset.s(1))
59-
assert "hi" is sym(lset.s(1), default="hi")
59+
assert "hi" == sym(lset.s(1), default="hi")
6060

6161
assert None is sym(lvector.v(1))
6262

0 commit comments

Comments
 (0)