Skip to content

Commit

Permalink
Merge pull request #1508 from khaeru/issue/1498
Browse files Browse the repository at this point in the history
Handle definitions @import from relative paths on Windows
  • Loading branch information
hgrecco authored Apr 19, 2022
2 parents 6de0f91 + f64ce35 commit f0f9002
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 79 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pint/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 str(pending_files[0].filename).endswith(str(definition.path)):
raise ValueError(
"The order of the files do not match. "
f"(expected: {definition.path}, "
Expand Down Expand Up @@ -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:
Expand Down
157 changes: 81 additions & 76 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f0f9002

Please sign in to comment.