summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHernan Grecco <hernan.grecco@gmail.com>2022-04-19 15:36:08 -0300
committerGitHub <noreply@github.com>2022-04-19 15:36:08 -0300
commitf0f9002fbbd5704dbd86cbcb397f6eac1217a8ef (patch)
tree4714f40706b02a24a954c77fbe270ae9a28100c6
parent6de0f9123c8d443bda8b02e1b1cfc56e4fb1e511 (diff)
parentf64ce35a8940a8a9f5ee18c894d57856e5f130b4 (diff)
downloadpint-f0f9002fbbd5704dbd86cbcb397f6eac1217a8ef.tar.gz
Merge pull request #1508 from khaeru/issue/14980.19.2
Handle definitions @import from relative paths on Windows
-rw-r--r--.github/workflows/ci.yml71
-rw-r--r--pint/parser.py6
-rw-r--r--pint/testsuite/test_issues.py157
3 files changed, 155 insertions, 79 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 4f0be8f..60ac53b 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
diff --git a/pint/parser.py b/pint/parser.py
index 91a7f66..e73e578 100644
--- a/pint/parser.py
+++ b/pint/parser.py
@@ -80,7 +80,7 @@ class DefinitionFiles(tuple):
f"No more files while trying to import {definition.path}."
)
- if not str(pending_files[0].filename).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 @@ def build_disk_cache_class(non_int_type: type):
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:
diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py
index ab65cfb..3609f43 100644
--- a/pint/testsuite/test_issues.py
+++ b/pint/testsuite/test_issues.py
@@ -902,102 +902,107 @@ if np is not None:
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