Skip to content

Comments

Preserve inexact dtype in Quantity construction (swev-id: astropy__astropy-8872)#105

Open
casey-brooks wants to merge 1 commit intoastropy__astropy-8872from
fix/quantity-float16-dtype-swev-astropy__astropy-8872
Open

Preserve inexact dtype in Quantity construction (swev-id: astropy__astropy-8872)#105
casey-brooks wants to merge 1 commit intoastropy__astropy-8872from
fix/quantity-float16-dtype-swev-astropy__astropy-8872

Conversation

@casey-brooks
Copy link

Summary

  • replace np.can_cast(np.float32, ...) checks with np.issubdtype(..., np.inexact) so inexact inputs keep their dtype
  • add regression coverage for float16 scalars/arrays, explicit dtype overrides, integer/bool casting, and other inexact dtypes

Issue Link

Reproduction (before fix)

Command:

SETUPTOOLS_USE_DISTUTILS=stdlib PYTHONPATH=. LD_LIBRARY_PATH=/nix/store/wffgswxkp55xi14jy63rjsnfvl2qvmxy-gcc-14.3.0-lib/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/zmjqwzhgl9hwr8xnk8raj8d50lzkql66-gcc-14.3.0-lib/lib:/nix/store/n5lymg0y5x6i9wipkjrsi8aczv1nr4qc-zlib-1.3.1/lib:${LD_LIBRARY_PATH} .venv/bin/pytest astropy/units/tests/test_quantity.py -k 'float16 or default_to_float64' -vv

Failure excerpt:

astropy/units/tests/test_quantity.py::TestQuantityCreation::test_float16_dtype_preservation FAILED
E       AssertionError: assert dtype('float64') == dtype('float16')

Testing (after fix)

SETUPTOOLS_USE_DISTUTILS=stdlib PYTHONPATH=. LD_LIBRARY_PATH=/nix/store/wffgswxkp55xi14jy63rjsnfvl2qvmxy-gcc-14.3.0-lib/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/zmjqwzhgl9hwr8xnk8raj8d50lzkql66-gcc-14.3.0-lib/lib:/nix/store/n5lymg0y5x6i9wipkjrsi8aczv1nr4qc-zlib-1.3.1/lib:${LD_LIBRARY_PATH} .venv/bin/pytest astropy/units/tests/test_quantity.py -k 'float16 or default_to_float64 or inexact' -vv
git diff --unified=0 origin/astropy__astropy-8872 -- astropy/units/quantity.py astropy/units/tests/test_quantity.py | SETUPTOOLS_USE_DISTUTILS=stdlib PYTHONPATH=. LD_LIBRARY_PATH=/nix/store/wffgswxkp55xi14jy63rjsnfvl2qvmxy-gcc-14.3.0-lib/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/zmjqwzhgl9hwr8xnk8raj8d50lzkql66-gcc-14.3.0-lib/lib:/nix/store/n5lymg0y5x6i9wipkjrsi8aczv1nr4qc-zlib-1.3.1/lib:${LD_LIBRARY_PATH} .venv/bin/flake8 --diff

@casey-brooks casey-brooks requested a review from a team December 18, 2025 17:39
@casey-brooks
Copy link
Author

Local test & lint results:

  • SETUPTOOLS_USE_DISTUTILS=stdlib PYTHONPATH=. LD_LIBRARY_PATH=/nix/store/wffgswxkp55xi14jy63rjsnfvl2qvmxy-gcc-14.3.0-lib/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/zmjqwzhgl9hwr8xnk8raj8d50lzkql66-gcc-14.3.0-lib/lib:/nix/store/n5lymg0y5x6i9wipkjrsi8aczv1nr4qc-zlib-1.3.1/lib:${LD_LIBRARY_PATH} .venv/bin/pytest astropy/units/tests/test_quantity.py -k 'float16 or default_to_float64 or inexact' -vv → 4 passed, 89 deselected
  • git diff --unified=0 origin/astropy__astropy-8872 -- astropy/units/quantity.py astropy/units/tests/test_quantity.py | SETUPTOOLS_USE_DISTUTILS=stdlib PYTHONPATH=. LD_LIBRARY_PATH=/nix/store/wffgswxkp55xi14jy63rjsnfvl2qvmxy-gcc-14.3.0-lib/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/zmjqwzhgl9hwr8xnk8raj8d50lzkql66-gcc-14.3.0-lib/lib:/nix/store/n5lymg0y5x6i9wipkjrsi8aczv1nr4qc-zlib-1.3.1/lib:${LD_LIBRARY_PATH} .venv/bin/flake8 --diff → no issues

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. Confirmed the iterparser move and table writer split behave as expected.

@casey-brooks
Copy link
Author

Tests:

  • `export LD_LIBRARY_PATH="$(printf '%s:' /nix/store/-gcc--lib/lib /nix/store/-gcc--libgcc/lib /nix/store/-zlib-/lib | sed 's/:$//'):$LD_LIBRARY_PATH" && SETUPTOOLS_USE_DISTUTILS=stdlib PYTHONPATH=. .venv/bin/pytest astropy/units/tests/test_quantity.py -k 'float16 or default_to_float64 or inexact' -vv` (4 passed, 89 deselected)
    Lint:
  • not run (not requested)

@rowan-stein rowan-stein changed the base branch from astropy__astropy-8872 to main January 11, 2026 18:37
@rowan-stein rowan-stein changed the base branch from main to astropy__astropy-8872 January 12, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants