Skip to content

Commit

Permalink
python.2: fix tests, use source checkers and more python3 features
Browse files Browse the repository at this point in the history
Check the whole code with flake8, pylint and mypy.

Report all possible errors with extensive context.

Demonstrate iterators, decorators, functional tools, chain maps,
dataclasses, match statements, assignments expressions.

Implement environments with python chain maps.

Rewrite the reader without external dependencies (but inspired by the
ptk library).  The motivation was that no external library is fully
type-checked for now.

Avoid name clashes when possible (print, read, readline, types).

Write the readline history file at exit, not after each prompt.

Replace printer.pr_str as methods of MAL objects.  This is idiomatic
python, and improves the error reporting.

Change some representations so that the python equality matches the
MAL equality.  The recursion is now implicit.

Remove -O from ./run.  It took me a while to understand that run-time
assertions were disabled!  MAL is about development, not performance.

Dispatch the special forms from a dict, for readability (pylint
rightfully complains that there are too many return statements in
eval_()).

Copy the recursion overflow fix, the python interaction from the first
python implementation.

Add tests detecting that nil false 0 () [] "" are distinct, and that 0
() are not False when tested (in python False == 0 and an empty
container is tested ).

Add tests checking that metadata does not affect equality (they do
with a naive python dataclass).
  • Loading branch information
asarhaddon committed Aug 22, 2024
1 parent 39563fd commit e7178e3
Show file tree
Hide file tree
Showing 25 changed files with 2,522 additions and 2,387 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -971,13 +971,10 @@ python stepX_YYY.py

### Python.2 (3.X)

The second Python implementation makes heavy use of type annotations and uses the Arpeggio parser library.

```
# Recommended: do these steps in a Python virtual environment.
pip3 install Arpeggio==1.9.0
python3 stepX_YYY.py
```
The second Python implementation is checked for style and types
(flake8, pylint, mypy). It reports all errors with details.
It demonstrates iterators, decorators, functional tools, chain maps,
dataclasses, introspection, match statements, assignement expressions.

### RPython

Expand Down
16 changes: 4 additions & 12 deletions impls/python.2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ MAINTAINER Joel Martin <[email protected]>
RUN apt-get -y update

# Required for running tests
RUN apt-get -y install make python

# Some typical implementation and test requirements
RUN apt-get -y install curl libreadline-dev libedit-dev
RUN apt-get -y install make python3
RUN ln -fs /usr/bin/python3 /usr/local/bin/python

RUN mkdir -p /mal
WORKDIR /mal
Expand All @@ -21,11 +19,5 @@ WORKDIR /mal
# Specific implementation requirements
##########################################################

# Nothing additional needed for python
RUN apt-get -y install python3

# For dist packaging
RUN apt-get -y install zip

# Pypi modules
RUN apt-get -y install python3-pip && pip3 install Arpeggio==1.9.0
# For checking:
# RUN apt-get -y install flake8 mypy pylint
24 changes: 22 additions & 2 deletions impls/python.2/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# make check sources=reader.py may be convenient
sources ?= *.py

f8 += D100 # Missing docstring in public module
f8 += D101 # Missing docstring in public class
f8 += D102 # Missing docstring in public method
f8 += D103 # Missing docstring in public function
f8 += D105 # Missing docstring in magic method
f8 += D107 # Missing docstring in __init__
f8 += I100 # order of import statements (incompatible with pylint)
f8 += W503 # line break before binary operator (incompatible with 504)
pl += missing-module-docstring
pl += missing-class-docstring
pl += missing-function-docstring
pl += R0801 # Similar lines in 2 files (steps...)

all:
true

.PHONY: clean
check:
pylint --disable=$(shell echo $(pl) | sed 's/ /,/g') $(sources)
mypy $(sources)
flake8 --ignore=$(shell echo $(f8) | sed 's/ /,/g') $(sources)

clean:
rm -f *~
rm -fr __pycache__/ .mypy_cache/
Loading

0 comments on commit e7178e3

Please sign in to comment.