Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpin numpy #56

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: 3.12
- name: Install tox
run: pip install tox
- name: Check style
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install tox
Expand Down
11 changes: 7 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: no-commit-to-branch
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/ambv/black
rev: 23.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.2
hooks:
- id: black
- id: ruff
args: [ --fix ]
- id: ruff-format
36 changes: 35 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ classifiers=[
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = ["numpy<2"]
dependencies = ["numpy"]
dynamic=["version"]

authors = [
Expand Down Expand Up @@ -49,3 +49,37 @@ dev = [
]

[tool.setuptools_scm]

[tool.ruff]
src = ["src"]
line-length = 88

[tool.ruff.lint]
select = [
"W", # pycodestyle
"I", # isort
"B", # flake-8-bugbear
"SIM", # flake-8-simplify
"F", # pyflakes
"PL", # pylint
"NPY", # numpy specific rules
"C4", # flake8-comprehensions
]
preview = true
ignore = ["PLW2901", # redefined-loop-name
"PLR2004", # magic-value-comparison
"PLR0915", # too-many-statements
"PLR0912", # too-many-branches
"PLR0911", # too-many-return-statements
"PLC2701", # import-private-name
"PLR6201", # literal-membership
"PLR0914", # too-many-locals
"PLR6301", # no-self-use
"PLW1641", # eq-without-hash
"PLR0904", # too-many-public-methods
"PLR1702", # too-many-nested-blocks
"PLW3201", # bad-dunder-method-name
]

[tool.ruff.lint.pylint]
max-args = 10
1 change: 1 addition & 0 deletions src/resfo/_formatted/write.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

import resfo.types as res_types
from resfo.errors import ResfoWriteError

Expand Down
6 changes: 2 additions & 4 deletions src/resfo/_unformatted/read.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io

import numpy as np

import resfo.types as res_types
from resfo._unformatted.common import bytes_in_array, group_len, item_size
from resfo._unformatted.write import write_array_like
Expand Down Expand Up @@ -51,10 +52,7 @@ def update(self, *, keyword=None, array=None):
keyword = self.read_keyword()

if array is not res_types.MESS:
if array is not None:
array = np.asarray(array)
else:
array = self.read_array()
array = np.asarray(array) if array is not None else self.read_array()
if array is not res_types.MESS and self.read_length() != array.size:
raise ValueError("Cannot update array with different size")

Expand Down
3 changes: 2 additions & 1 deletion src/resfo/_unformatted/write.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

import numpy as np

import resfo.types as res_types
from resfo._unformatted.common import group_len, item_size
from resfo.errors import ResfoWriteError
Expand Down Expand Up @@ -35,7 +36,7 @@ def cast_array_to_res(arr):
else:
raise ValueError(f"Cannot cast {arr.dtype} to a res type")

warnings.warn(f"casting array dtype {arr.dtype} to {result_dtype}")
warnings.warn(f"casting array dtype {arr.dtype} to {result_dtype}", stacklevel=1)
return arr.astype(result_dtype)


Expand Down
4 changes: 2 additions & 2 deletions src/resfo/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def get_stream(filepath, fileformat: Format, mode: str = "r"):
"""
if isinstance(filepath, (str, Path)):
if fileformat == Format.FORMATTED:
return open(filepath, mode + "t"), True
return open(filepath, mode + "t"), True # noqa: SIM115
else:
return open(filepath, mode + "b"), True
return open(filepath, mode + "b"), True # noqa: SIM115
else:
return filepath, False

Expand Down
7 changes: 4 additions & 3 deletions src/resfo/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
that results in a warning.

"""

import warnings
from typing import TYPE_CHECKING, Union

Expand Down Expand Up @@ -61,7 +62,7 @@ def to_np_type(type_keyword):
"""
if type_keyword[0:2] == b"C0":
return np.dtype("|S" + type_keyword[2:4].decode("ascii"))
return static_dtypes.get(type_keyword, None)
return static_dtypes.get(type_keyword)


def from_np_dtype(array):
Expand All @@ -78,7 +79,7 @@ def from_np_dtype(array):
if dtype in [np.dtype(np.int32), np.dtype(np.int32).newbyteorder(">")]:
return b"INTE"
if dtype in [np.dtype(np.int64), np.dtype(np.int64).newbyteorder(">")]:
warnings.warn("downcasting numpy int64 to int32 for res file.")
warnings.warn("downcasting numpy int64 to int32 for res file.", stacklevel=1)
return b"INTE"
if dtype in [np.dtype(np.float32), np.dtype(np.float32).newbyteorder(">")]:
return b"REAL"
Expand Down Expand Up @@ -112,7 +113,7 @@ def is_valid_type(type_str):
"""
:returns: Whether the given byte string is a valid res type.
"""
if type_str in static_dtypes.keys():
if type_str in static_dtypes:
return True
if type_str in [b"X231", b"MESS"]:
return True
Expand Down
2 changes: 1 addition & 1 deletion src/resfo/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from importlib.metadata import version, PackageNotFoundError
from importlib.metadata import PackageNotFoundError, version

try:
version = version("resfo")
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from hypothesis import HealthCheck, settings


settings.register_profile(
"no_timeouts",
deadline=None,
Expand Down
3 changes: 2 additions & 1 deletion tests/generators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import hypothesis.strategies as st
import numpy as np
import resfo
from hypothesis.extra.numpy import arrays

import resfo

formats = st.sampled_from(resfo.Format)
in_formats = st.one_of(formats, st.just(None))
keywords = st.text(
Expand Down
1 change: 1 addition & 0 deletions tests/test_error_handling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from io import BytesIO, StringIO

import pytest

from resfo import Format, ResfoParsingError, ResfoWriteError, read, write


Expand Down
3 changes: 2 additions & 1 deletion tests/test_formatted_common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import hypothesis.strategies as st
import pytest
from resfo._unformatted.common import bytes_in_array, group_len, item_size
from hypothesis import given

from resfo._unformatted.common import bytes_in_array, group_len, item_size


def test_group_len():
assert group_len(b"C032") == 105
Expand Down
1 change: 1 addition & 0 deletions tests/test_formatted_read.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io

import numpy as np

import resfo._formatted.read as fread


Expand Down
1 change: 1 addition & 0 deletions tests/test_formatted_write.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io

import numpy as np

import resfo._formatted.write as fwrite
from resfo.types import MESS

Expand Down
1 change: 1 addition & 0 deletions tests/test_read_write.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from hypothesis import HealthCheck, given, settings
from numpy.testing import assert_allclose

from resfo import MESS, read, write

from .generators import formats, resfo_datas
Expand Down
9 changes: 5 additions & 4 deletions tests/test_type_resolution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest

import resfo


Expand All @@ -19,7 +20,7 @@ def test_formatted_read_type_resolution(tmp_path, contents, expected_type):
f.write(contents)

with (tmp_path / "test.txt").open("r") as f:
((specgrid, arr),) = resfo.read(f)
((_specgrid, arr),) = resfo.read(f)

assert arr.dtype == expected_type

Expand Down Expand Up @@ -77,9 +78,9 @@ def keyword_start(res_type):
),
(
keyword_start(b"C010")
+ b"\x00\x00\x00\x0A"
+ b"\x00\x00\x00\x0a"
+ b"SPECGRID10"
+ b"\x00\x00\x00\x0A",
+ b"\x00\x00\x00\x0a",
np.dtype("|S10"),
),
(
Expand All @@ -94,7 +95,7 @@ def test_unformatted_read_type_resolution(tmp_path, contents, expected_type):
f.write(contents)

with (tmp_path / "test.txt").open("rb") as f:
((specgrid, arr),) = resfo.read(f)
((_specgrid, arr),) = resfo.read(f)

assert arr.dtype == expected_type

Expand Down
1 change: 1 addition & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

import resfo.types


Expand Down
1 change: 1 addition & 0 deletions tests/test_unformatted_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest

import resfo._unformatted.read as uwrite


Expand Down
1 change: 1 addition & 0 deletions tests/test_unformatted_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest

import resfo._unformatted.write as uwrite
from resfo.types import MESS

Expand Down
2 changes: 1 addition & 1 deletion tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from resfo import MESS, lazy_read, read, write

from .generators import resfo_datas, float_arrays, keywords
from .generators import float_arrays, keywords, resfo_datas


@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
Expand Down