From 476cce2070496e836c6d3b4787686bb9734cc293 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 11 Apr 2022 10:18:52 +0200 Subject: [PATCH 1/4] Use Windows-friendly path checking in DefinitionFiles._iter_definitions --- pint/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint/parser.py b/pint/parser.py index 91a7f664b..636c26c17 100644 --- a/pint/parser.py +++ b/pint/parser.py @@ -80,7 +80,7 @@ def _iter_definitions( f"No more files while trying to import {definition.path}." ) - if not str(pending_files[0].filename).endswith(definition.path): + if not pending_files[0].filename.as_posix().endswith(definition.path): raise ValueError( "The order of the files do not match. " f"(expected: {definition.path}, " From 3ae2241232edcf053b64fdf602338fcdf24a608d Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 11 Apr 2022 10:20:58 +0200 Subject: [PATCH 2/4] Adjust test_issue1498b to use subdirectories Use provided .testsuite.helpers.require_numpy instead of enclosing tests in an if block. --- pint/testsuite/test_issues.py | 157 ++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index ab65cfbdc..3609f43a9 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -902,102 +902,107 @@ def test_issue925(module_registry, callable, q_params): callable(q) assert isinstance(q._magnitude, type_before) - def test_issue1498(tmp_path): - def0 = tmp_path / "def0.txt" - def1 = tmp_path / "def1.txt" - def2 = tmp_path / "def2.txt" - # A file that defines a new base unit and uses it in a context - def0.write_text( - """ - foo = [FOO] +@helpers.requires_numpy +def test_issue1498(tmp_path): + def0 = tmp_path / "def0.txt" + def1 = tmp_path / "def1.txt" + def2 = tmp_path / "def2.txt" - @context BAR - [FOO] -> [mass]: value / foo * 10.0 kg - @end + # A file that defines a new base unit and uses it in a context + def0.write_text( """ - ) + foo = [FOO] - # A file that defines a new base unit, then imports another file… - def1.write_text( - f""" - foo = [FOO] + @context BAR + [FOO] -> [mass]: value / foo * 10.0 kg + @end + """ + ) - @import {str(def2)} - """ - ) + # A file that defines a new base unit, then imports another file… + def1.write_text( + f""" + foo = [FOO] - # …that, in turn, uses it in a context - def2.write_text( - """ - @context BAR - [FOO] -> [mass]: value / foo * 10.0 kg - @end - """ - ) + @import {str(def2)} + """ + ) - # Succeeds with pint 0.18; fails with pint 0.19 - ureg1 = UnitRegistry() - ureg1.load_definitions(def1) # ← FAILS + # …that, in turn, uses it in a context + def2.write_text( + """ + @context BAR + [FOO] -> [mass]: value / foo * 10.0 kg + @end + """ + ) - assert 12.0 == ureg1("1.2 foo").to("kg", "BAR").magnitude + # Succeeds with pint 0.18; fails with pint 0.19 + ureg1 = UnitRegistry() + ureg1.load_definitions(def1) # ← FAILS - def test_issue1498b(tmp_path): - def0 = tmp_path / "def0.txt" - def1 = tmp_path / "def1.txt" - def1_1 = tmp_path / "def1_1.txt" - def1_2 = tmp_path / "def1_2.txt" - def2 = tmp_path / "def2.txt" + assert 12.0 == ureg1("1.2 foo").to("kg", "BAR").magnitude - # A file that defines a new base unit and uses it in a context - def0.write_text( - """ - foo = [FOO] - @context BAR - [FOO] -> [mass]: value / foo * 10.0 kg - @end +@helpers.requires_numpy +def test_issue1498b(tmp_path): + def0 = tmp_path / "def0.txt" + def1 = tmp_path / "dir_a" / "def1.txt" + def1_1 = tmp_path / "dir_a" / "def1_1.txt" + def1_2 = tmp_path / "dir_a" / "def1_2.txt" + def2 = tmp_path / "def2.txt" - @import def1.txt - @import def2.txt + # A file that defines a new base unit and uses it in a context + def0.write_text( """ - ) + foo = [FOO] - # A file that defines a new base unit, then imports another file… - def1.write_text( - """ - @import def1_1.txt - @import def1_2.txt + @context BAR + [FOO] -> [mass]: value / foo * 10.0 kg + @end + + @import dir_a/def1.txt + @import def2.txt + """ + ) + + # A file that defines a new base unit, then imports another file… + def1.parent.mkdir() + def1.write_text( """ - ) + @import def1_1.txt + @import def1_2.txt + """ + ) - def1_1.write_text( - """ - @context BAR1_1 - [FOO] -> [mass]: value / foo * 10.0 kg - @end + def1_1.write_text( """ - ) + @context BAR1_1 + [FOO] -> [mass]: value / foo * 10.0 kg + @end + """ + ) - def1_2.write_text( - """ - @context BAR1_2 - [FOO] -> [mass]: value / foo * 10.0 kg - @end + def1_2.write_text( """ - ) + @context BAR1_2 + [FOO] -> [mass]: value / foo * 10.0 kg + @end + """ + ) - # …that, in turn, uses it in a context - def2.write_text( - """ - @context BAR2 - [FOO] -> [mass]: value / foo * 10.0 kg - @end + # …that, in turn, uses it in a context + def2.write_text( """ - ) + @context BAR2 + [FOO] -> [mass]: value / foo * 10.0 kg + @end + """ + ) - # Succeeds with pint 0.18; fails with pint 0.19 - ureg1 = UnitRegistry() - ureg1.load_definitions(def0) # ← FAILS + # Succeeds with pint 0.18; fails with pint 0.19 + ureg1 = UnitRegistry() + ureg1.load_definitions(def0) # ← FAILS - assert 12.0 == ureg1("1.2 foo").to("kg", "BAR").magnitude + assert 12.0 == ureg1("1.2 foo").to("kg", "BAR").magnitude From 7111d87a88b7851a1c37708801103bd4b36186a1 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 11 Apr 2022 10:24:12 +0200 Subject: [PATCH 3/4] Add test-windows GitHub Actions jobs in ci.yaml --- .github/workflows/ci.yml | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4f0be8f77..60ac53bec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,6 +88,77 @@ jobs: pip install coveralls coveralls + test-windows: + strategy: + fail-fast: false + matrix: + python-version: [3.8, 3.9, "3.10"] + numpy: [ "numpy>=1.19,<2.0.0" ] + # uncertainties: [null, "uncertainties==3.1.6", "uncertainties>=3.1.6,<4.0.0"] + # extras: [null] + # include: + # - python-version: 3.8 # Minimal versions + # numpy: numpy==1.19.5 + # extras: matplotlib==2.2.5 + # - python-version: 3.8 + # numpy: "numpy" + # uncertainties: "uncertainties" + # extras: "sparse xarray netCDF4 dask[complete] graphviz babel==2.8" + runs-on: windows-latest + + env: + TEST_OPTS: "-rfsxEX -s -k issue1498b" + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 100 + + - name: Get tags + run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + - name: Get pip cache dir + id: pip-cache + run: echo "::set-output name=dir::$(pip cache dir)" + + - name: Setup caching + uses: actions/cache@v2 + with: + path: ${{ steps.pip-cache.outputs.dir }} + key: pip-windows-${{ matrix.python-version }} + restore-keys: | + pip-windows-${{ matrix.python-version }} + + - name: Install numpy + if: ${{ matrix.numpy != null }} + run: pip install "${{matrix.numpy}}" + + # - name: Install uncertainties + # if: ${{ matrix.uncertainties != null }} + # run: pip install "${{matrix.uncertainties}}" + # + # - name: Install extras + # if: ${{ matrix.extras != null }} + # run: pip install ${{matrix.extras}} + + - name: Install dependencies + run: | + # sudo apt install -y graphviz + pip install pytest pytest-cov pytest-subtests + pip install . + + # - name: Install pytest-mpl + # if: contains(matrix.extras, 'matplotlib') + # run: pip install pytest-mpl + + - name: Run tests + run: pytest ${env:TEST_OPTS} + coveralls: needs: test-linux runs-on: ubuntu-latest From f64ce35a8940a8a9f5ee18c894d57856e5f130b4 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 11 Apr 2022 11:50:21 +0200 Subject: [PATCH 4/4] Change type of ImportDefinition.path to pathlib.Path --- pint/parser.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pint/parser.py b/pint/parser.py index 636c26c17..e73e57889 100644 --- a/pint/parser.py +++ b/pint/parser.py @@ -80,7 +80,7 @@ def _iter_definitions( f"No more files while trying to import {definition.path}." ) - if not pending_files[0].filename.as_posix().endswith(definition.path): + if not str(pending_files[0].filename).endswith(str(definition.path)): raise ValueError( "The order of the files do not match. " f"(expected: {definition.path}, " @@ -135,13 +135,13 @@ class PintDiskCache(fc.DiskCache): class ImportDefinition: """Definition for the @import directive""" - path: str + path: pathlib.Path @classmethod def from_string( cls, definition: str, non_int_type: type = float ) -> ImportDefinition: - return ImportDefinition(definition[7:].strip()) + return ImportDefinition(pathlib.Path(definition[7:].strip())) class Parser: