tests: Format all Python code with black, except tests in basics subdir.

This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
This commit is contained in:
David Lechner
2020-03-22 21:26:08 -05:00
committed by Damien George
parent 488613bca6
commit 3dc324d3f1
472 changed files with 4396 additions and 2891 deletions

View File

@@ -6,9 +6,11 @@ workaround: Unknown
"""
import gc
class Foo():
class Foo:
def __del__(self):
print('__del__')
print("__del__")
f = Foo()
del f

View File

@@ -4,12 +4,16 @@ description: Method Resolution Order (MRO) is not compliant with CPython
cause: Depth first non-exhaustive method resolution order
workaround: Avoid complex class hierarchies with multiple inheritance and complex method overrides. Keep in mind that many languages don't support multiple inheritance at all.
"""
class Foo:
def __str__(self):
return "Foo"
class C(tuple, Foo):
pass
t = C((1, 2, 3))
print(t)

View File

@@ -4,24 +4,29 @@ description: When inheriting from multiple classes super() only calls one class
cause: See :ref:`cpydiff_core_class_mro`
workaround: See :ref:`cpydiff_core_class_mro`
"""
class A:
def __init__(self):
print("A.__init__")
class B(A):
def __init__(self):
print("B.__init__")
super().__init__()
class C(A):
def __init__(self):
print("C.__init__")
super().__init__()
class D(B,C):
class D(B, C):
def __init__(self):
print("D.__init__")
super().__init__()
D()

View File

@@ -4,15 +4,19 @@ description: Calling super() getter property in subclass will return a property
cause: Unknown
workaround: Unknown
"""
class A:
@property
def p(self):
return {"a":10}
return {"a": 10}
class AA(A):
@property
def p(self):
return super().p
a = AA()
print(a.p)

View File

@@ -4,8 +4,11 @@ description: User-defined attributes for functions are not supported
cause: MicroPython is highly optimized for memory usage.
workaround: Use external dictionary, e.g. ``FUNC_X[f] = 0``.
"""
def f():
pass
f.x = 0
print(f.x)

View File

@@ -4,11 +4,15 @@ description: Context manager __exit__() not called in a generator which does not
cause: Unknown
workaround: Unknown
"""
class foo(object):
def __enter__(self):
print('Enter')
print("Enter")
def __exit__(self, *args):
print('Exit')
print("Exit")
def bar(x):
with foo():
@@ -16,9 +20,11 @@ def bar(x):
x += 1
yield x
def func():
g = bar(0)
for _ in range(3):
print(next(g))
func()

View File

@@ -12,6 +12,7 @@ except NameError as e:
print(e)
try:
from modules import foo
print('Should not get here')
print("Should not get here")
except NameError as e:
print(e)

View File

@@ -5,6 +5,7 @@ cause: MicroPython's import system is highly optimized for simplicity, minimal m
workaround: Don't install modules belonging to the same namespace package in different directories. For MicroPython, it's recommended to have at most 3-component module search paths: for your current application, per-user (writable), system-wide (non-writable).
"""
import sys
sys.path.append(sys.path[1] + "/modules")
sys.path.append(sys.path[1] + "/modules2")

View File

@@ -4,8 +4,11 @@ description: Local variables aren't included in locals() result
cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name.
workaround: Unknown
"""
def test():
val = 2
print(locals())
test()

View File

@@ -6,9 +6,11 @@ workaround: Unknown
"""
val = 1
def test():
val = 2
print(val)
eval("print(val)")
test()

View File

@@ -1,2 +1,2 @@
print('foo')
print("foo")
xxx

View File

@@ -5,4 +5,5 @@ cause: Unknown
workaround: Unknown
"""
import array
print(1 in array.array('B', b'12'))
print(1 in array.array("B", b"12"))

View File

@@ -5,6 +5,7 @@ cause: Unknown
workaround: Unknown
"""
import array
a = array.array('b', (1, 2, 3))
a = array.array("b", (1, 2, 3))
del a[1]
print(a)

View File

@@ -5,5 +5,6 @@ cause: Unknown
workaround: Unknown
"""
import array
a = array.array('b', (1, 2, 3))
a = array.array("b", (1, 2, 3))
print(a[3:2:2])

View File

@@ -5,5 +5,6 @@ cause: Unknown
workaround: Use regular lists. micropython-lib has implementation of collections.deque.
"""
import collections
D = collections.deque()
print(D)

View File

@@ -5,10 +5,11 @@ cause: Unknown
workaround: Unknown
"""
import json
a = bytes(x for x in range(256))
try:
z = json.dumps(a)
x = json.loads(z)
print('Should not get here')
print("Should not get here")
except TypeError:
print('TypeError')
print("TypeError")

View File

@@ -5,12 +5,13 @@ cause: Unknown
workaround: Use ``getenv``, ``putenv`` and ``unsetenv``
"""
import os
try:
print(os.environ.get('NEW_VARIABLE'))
os.environ['NEW_VARIABLE'] = 'VALUE'
print(os.environ['NEW_VARIABLE'])
print(os.environ.get("NEW_VARIABLE"))
os.environ["NEW_VARIABLE"] = "VALUE"
print(os.environ["NEW_VARIABLE"])
except AttributeError:
print('should not get here')
print(os.getenv('NEW_VARIABLE'))
os.putenv('NEW_VARIABLE', 'VALUE')
print(os.getenv('NEW_VARIABLE'))
print("should not get here")
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))

View File

@@ -5,6 +5,7 @@ cause: The ``environ`` attribute is not implemented
workaround: Unknown
"""
import os
print(os.getenv('NEW_VARIABLE'))
os.putenv('NEW_VARIABLE', 'VALUE')
print(os.getenv('NEW_VARIABLE'))
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))

View File

@@ -5,9 +5,10 @@ cause: Unknown
workaround: Test that the return value is ``None``
"""
import os
try:
print(os.getenv('NEW_VARIABLE', 'DEFAULT'))
print(os.getenv("NEW_VARIABLE", "DEFAULT"))
except TypeError:
print('should not get here')
print("should not get here")
# this assumes NEW_VARIABLE is never an empty variable
print(os.getenv('NEW_VARIABLE') or 'DEFAULT')
print(os.getenv("NEW_VARIABLE") or "DEFAULT")

View File

@@ -5,8 +5,9 @@ cause: Unknown
workaround: Unknown
"""
import struct
try:
print(struct.pack('bb', 1))
print('Should not get here')
print(struct.pack("bb", 1))
print("Should not get here")
except:
print('struct.error')
print("struct.error")

View File

@@ -5,8 +5,9 @@ cause: Unknown
workaround: Unknown
"""
import struct
try:
print(struct.pack('bb', 1, 2, 3))
print('Should not get here')
print(struct.pack("bb", 1, 2, 3))
print("Should not get here")
except:
print('struct.error')
print("struct.error")

View File

@@ -5,5 +5,6 @@ cause: They are stored in read-only memory.
workaround: Unknown
"""
import sys
sys.stdin = None
print(sys.stdin)

View File

@@ -5,14 +5,14 @@ cause: Unknown
workaround: Unknown
"""
try:
print(eval('1and 0'))
print(eval("1and 0"))
except SyntaxError:
print('Should have worked')
print("Should have worked")
try:
print(eval('1or 0'))
print(eval("1or 0"))
except SyntaxError:
print('Should have worked')
print("Should have worked")
try:
print(eval('1if 1else 0'))
print(eval("1if 1else 0"))
except SyntaxError:
print('Should have worked')
print("Should have worked")

View File

@@ -4,4 +4,4 @@ description: bytes objects support .format() method
cause: MicroPython strives to be a more regular implementation, so if both `str` and `bytes` support ``__mod__()`` (the % operator), it makes sense to support ``format()`` for both too. Support for ``__mod__`` can also be compiled out, which leaves only ``format()`` for bytes formatting.
workaround: If you are interested in CPython compatibility, don't use ``.format()`` on bytes objects.
"""
print(b'{}'.format(1))
print(b"{}".format(1))

View File

@@ -4,4 +4,4 @@ description: bytes() with keywords not implemented
cause: Unknown
workaround: Pass the encoding as a positional parameter, e.g. ``print(bytes('abc', 'utf-8'))``
"""
print(bytes('abc', encoding='utf8'))
print(bytes("abc", encoding="utf8"))

View File

@@ -4,4 +4,4 @@ description: Bytes subscription with step != 1 not implemented
cause: MicroPython is highly optimized for memory usage.
workaround: Use explicit loop for this very rare operation.
"""
print(b'123'[0:3:2])
print(b"123"[0:3:2])

View File

@@ -4,4 +4,4 @@ description: Dictionary keys view does not behave as a set.
cause: Not implemented.
workaround: Explicitly convert keys to a set before using set operations.
"""
print({1:2, 3:4}.keys() & {1})
print({1: 2, 3: 4}.keys() & {1})

View File

@@ -8,8 +8,11 @@ workaround: Call using ``super()`` instead::
def __init__(self):
super().__init__()
"""
class A(Exception):
def __init__(self):
Exception.__init__(self)
a = A()

View File

@@ -4,4 +4,4 @@ description: uPy and CPython outputs formats may differ
cause: Unknown
workaround: Unknown
"""
print('%.1g' % -9.9)
print("%.1g" % -9.9)

View File

@@ -4,8 +4,11 @@ description: No int conversion for int-derived types available
cause: Unknown
workaround: Avoid subclassing builtin types unless really needed. Prefer https://en.wikipedia.org/wiki/Composition_over_inheritance .
"""
class A(int):
__add__ = lambda self, other: A(int(self) + other)
a = A(42)
print(a+a)
print(a + a)

View File

@@ -4,4 +4,4 @@ description: Start/end indices such as str.endswith(s, start) not implemented
cause: Unknown
workaround: Unknown
"""
print('abc'.endswith('c', 1))
print("abc".endswith("c", 1))

View File

@@ -4,4 +4,4 @@ description: Attributes/subscr not implemented
cause: Unknown
workaround: Unknown
"""
print('{a[0]}'.format(a=[1, 2]))
print("{a[0]}".format(a=[1, 2]))

View File

@@ -4,4 +4,4 @@ description: str(...) with keywords not implemented
cause: Unknown
workaround: Input the encoding format directly. eg ``print(bytes('abc', 'utf-8'))``
"""
print(str(b'abc', encoding='utf8'))
print(str(b"abc", encoding="utf8"))

View File

@@ -4,4 +4,4 @@ description: str.ljust() and str.rjust() not implemented
cause: MicroPython is highly optimized for memory usage. Easy workarounds available.
workaround: Instead of ``s.ljust(10)`` use ``"%-10s" % s``, instead of ``s.rjust(10)`` use ``"% 10s" % s``. Alternatively, ``"{:<10}".format(s)`` or ``"{:>10}".format(s)``.
"""
print('abc'.ljust(10))
print("abc".ljust(10))

View File

@@ -4,4 +4,4 @@ description: None as first argument for rsplit such as str.rsplit(None, n) not i
cause: Unknown
workaround: Unknown
"""
print('a a a'.rsplit(None, 1))
print("a a a".rsplit(None, 1))

View File

@@ -4,4 +4,4 @@ description: Subscript with step != 1 is not yet implemented
cause: Unknown
workaround: Unknown
"""
print('abcdefghi'[0:9:2])
print("abcdefghi"[0:9:2])