Skip to content

BUG: missing support for Python optimized mode #17471

Open

Description

@neutrinoceros

Description

The regression I'm seeing is clearly from #17118, but wasn't spotted because we don't have a test job for it. Fixing the crash itself should be simple but we should probably add some sort of regular testing for it, probably in weekly-cron.

Expected behavior

No response

How to Reproduce

$ python -OO -c "from astropy.io import *"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/clm/dev/astropy-project/coordinated/astropy/astropy/__init__.py", line 23, in <module>
    from . import config as _config
  File "/Users/clm/dev/astropy-project/coordinated/astropy/astropy/config/__init__.py", line 8, in <module>
    from .configuration import *
  File "/Users/clm/dev/astropy-project/coordinated/astropy/astropy/config/configuration.py", line 27, in <module>
    from .paths import get_config_dir_path
  File "/Users/clm/dev/astropy-project/coordinated/astropy/astropy/config/paths.py", line 78, in <module>
    get_config_dir_path.__doc__
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Versions

platform
--------
platform.platform() = 'macOS-15.1.1-arm64-arm-64bit'
platform.version() = 'Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:11 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6020'
platform.python_version() = '3.11.10'

packages
--------
astropy              7.1.dev202+g5d51416f46
numpy                2.1.3
scipy                1.14.1
matplotlib           3.9.2
pandas               2.2.3
pyerfa               2.0.1.5

for reference, progress is being tracked on the linked PR #17472

Activity

  1. changed the title [-]BUG: (7.0.0 regression) some subpackages cannot import under Python's optimized mode[/-] [+]BUG: missing support for Python optimized mode[/+] on Nov 29, 2024
  2. neutrinoceros commented on Nov 29, 2024

    @neutrinoceros
    ContributorAuthor

    I broaden the scope of the issue via the title as I'm discovering many (small) problems, most not new, with running astropy under python -OO. Several PRs will be needed, I think.

  3. neutrinoceros commented on Nov 29, 2024

    @neutrinoceros
    ContributorAuthor

    Looks like this already happened before: #15028

  4. eerovaher commented on Nov 29, 2024

    @eerovaher
    Member

    The Python optimization levels are a mechanism that allows users to disable assert statements and docstrings. We cannot prevent users from setting the optimization levels, so using assert statements in business logic or relying on the existence of docstrings can cause bugs.

    The Ruff rule S101 (assert) guards against assert statements. We are currently not enforcing it, but we really should:

    "S101", # Use of `assert` detected

    We already have a well-justified exception for tests:

    astropy/pyproject.toml

    Lines 399 to 403 in 7c800f8

    "test_*.py" = [
    "ANN201", # Public function without return type annotation
    "B018", # UselessExpression
    "D", # pydocstyle
    "S101", # Use of assert detected

    The potentially invalid uses of docstrings can be guarded against with type checking because the type of a docstring is str | None, so type checkers wont let the None case to go unnoticed. Admittedly, we can't rely on that until astropy is fully annotated and at the rate we are annotating the code this will take many decades.

  5. mhvk commented on Nov 29, 2024

    @mhvk
    Contributor

    Agreed that we can turn on that ruff rule. For the presumably very few cases where it is just to help indicate expectations, one can add a noqa. Let's not replace things with raise AssertionError, though!

  6. neutrinoceros commented on Nov 29, 2024

    @neutrinoceros
    ContributorAuthor

    I think assert statements in the code base are okay as long as they are not an integral part of business logic.
    In fact, running ruff check astropy --select S101 gets you 41 violations as of yesterday's main branch; but besides the 3 that are already being addressed in #17476 and #17477, I only find one that's apparently relied on, though it doesn't cause any test to fail if one removes it:

    assert self.unit.is_unity()

    I'm not sure it's worth all the work needed to actually enforce this rule. We should already be spotting the important problems by running the test suite in optimized mode, as is proposed in #17472

  7. eerovaher commented on Nov 29, 2024

    @eerovaher
    Member

    If there are assert statements in our code that don't cause test failures with optimizations enabled then that means either our tests are not as complete as they should be or there is dead code we should remove.

  8. WilliamJamieson commented on Nov 29, 2024

    @WilliamJamieson
    Contributor

    I agree that we enforce S101, but we will need to exclude all the test files.

  9. neutrinoceros commented on Nov 29, 2024

    @neutrinoceros
    ContributorAuthor

    If there are assert statements in our code that don't cause test failures with optimizations enabled then that means either our tests are not as complete as they should be or there is dead code we should remove.

    True, but either way could be significant amount of work with unclear gains. To be clear, if there's a consensus that it's worth doing, I'm okay to go with it, I'm just worried that coding purity may not be a consensual priority to the project.

    I agree that we enforce S101, but we will need to exclude all the test files.

    it's not necessary, this rule already ignores test files.

  10. mhvk commented on Nov 29, 2024

    @mhvk
    Contributor

    That units one should definitely be rewritten (and a test added trying to get the index for a non-dimensionless quantity). Currently, on astropy 5.2.1 (where running python -OO is OK...):

    >>> import astropy.units as u
    >>> import operator
    >>> operator.index(u.Quantity(10, u.one, dtype=int))
    10
    >>> operator.index(u.Quantity(10, u.m, dtype=int))
    10
    

    The last one raises TypeError without -OO.

    This example does make me think it is worthwhile to check that the assert we use are actually not important for the logic...

  11. neutrinoceros commented on Nov 29, 2024

    @neutrinoceros
    ContributorAuthor

    Thanks; I'm adding the missing test (and fix) in #17481

  12. pllim commented on Dec 9, 2024

    @pllim
    Member

    Is this a duplicate of #15052 ?

  13. added
    Close?Tell stale bot that this issue/PR is stale
    and removed
    Close?Tell stale bot that this issue/PR is stale
    on Dec 9, 2024
  14. neutrinoceros commented on Dec 9, 2024

    @neutrinoceros
    ContributorAuthor

    More of an extension and a revival, since there were no systematic check introduced and most problems I caught while hunting this actually predate your issue.

  15. pllim commented on Dec 9, 2024

    @pllim
    Member

    Maybe I should close the other one as duplicate then. Thanks!

  16. marscode-agent commented on Dec 11, 2024

    @marscode-agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions