From 7aa50b16d5b25e02bd14492a3b7efa9b01f5d404 Mon Sep 17 00:00:00 2001 From: Ali Ebrahim Date: Sat, 28 Oct 2023 15:06:30 +0000 Subject: [PATCH] Run tests with -O and -OO. --- .github/workflows/test.yml | 2 ++ once_test.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 42cb5ed..3d284b7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,6 +20,8 @@ jobs: cache: pip - run: pip install pytest - run: pytest . --junitxml=junit/test_py${{ matrix.python-version }}_on_${{ matrix.os }}.xml + - run: python -O once_test.py + - run: python -OO once_test.py - name: Upload pytest test results uses: actions/upload-artifact@v3 if: success() || failure() diff --git a/once_test.py b/once_test.py index 1fa83d5..74e096f 100644 --- a/once_test.py +++ b/once_test.py @@ -526,16 +526,22 @@ def closure(): self.assertIsNone(ephemeral_ref()) def test_function_signature_preserved(self): - @once.once def type_annotated_fn(arg: float) -> int: """Very descriptive docstring.""" del arg return 1 - sig = inspect.signature(type_annotated_fn) - self.assertIs(sig.parameters["arg"].annotation, float) - self.assertIs(sig.return_annotation, int) - self.assertEqual(type_annotated_fn.__doc__, "Very descriptive docstring.") + decorated_function = once.once(type_annotated_fn) + original_sig = inspect.signature(type_annotated_fn) + decorated_sig = inspect.signature(decorated_function) + self.assertIs(original_sig.parameters["arg"].annotation, float) + self.assertIs(decorated_sig.parameters["arg"].annotation, float) + self.assertIs(original_sig.return_annotation, int) + self.assertIs(decorated_sig.return_annotation, int) + self.assertEqual(inspect.getdoc(type_annotated_fn), inspect.getdoc(decorated_function)) + if sys.flags.optimize >= 2: + self.skipTest("docstrings get stripped with -OO") + self.assertEqual(inspect.getdoc(type_annotated_fn), "Very descriptive docstring.") def test_once_per_class(self): class _CallOnceClass(Counter):