Skip to content

Filtering AstropyDeprecationWarning with PYTHONWARNINGS env. #9832

Closed

Description

@rainwoodman
$ export PYTHONWARNINGS=ignore::AstropyDeprecationWarning
$ python
Invalid -W option ignored: unknown warning category: 'AstropyDeprecationWarning'
...
>>

So apparently python only allows built-in category names from the environment variable.
However, AstropyDeprecationWarning is not a subclass of the built-in DeprecationWarning. Thus it seems like there is no way to filter on the category from the environment variable.

Maybe this is because I do not know how to use PYTHONWARNINGS environment -- any suggestions?

Activity

  1. pllim commented on Jan 3, 2020

    @pllim
    Member

    Hi, @rainwoodman ! To better assist you, we would need to understand your use case a little more. Are you trying to filter it out inside your package's conftest.py (or some other file inside your package) or as part of a non-package script?

  2. saimn commented on Jan 4, 2020

    @saimn
    Contributor

    It should be possible to use something like python -W ignore::astropy.utils.exceptions.AstropyDeprecationWarning or maybe python -W ignore::AstropyDeprecationWarning:stropy.utils.exceptions: but there is a Python bug that prevents this :
    https://bugs.python.org/issue22543
    https://stackoverflow.com/questions/42495641/filtering-based-on-custom-warning-categories
    It seems that the import fails because it is done before site-packages are enabled.

  3. mhvk commented on Jan 4, 2020

    @mhvk
    Contributor

    That same place also gives a work-around:
    "The (currently only and accepted) answer proposes as a workaround to set the PYTHONPATH variable, as the contents of those apparently are already in sys.path at the time warnings._processoptions is called, unlike site-packages. So manually adding site-packages to the PYTHONPATH variable does circumvent this problem (tested in Python 3.7.1)."

    I'm not sure, though, why we are not subclassing from DeprecationWarning - we do subclass our AstropyPendingDeprecationWarning properly. Maybe @astrofrog knows?

  4. rainwoodman commented on Jan 5, 2020

    @rainwoodman
    Author

    Thanks for the link and the PYTHONPATH workaround.

    More about the use case: I am running an MPI application on 2048 ranks -- the warning (due to importing astropy/halotools) buries the useful logs coming out from the application. Thus I am adding the warnings filter as an environment variable in the job script as a non-invasive patchwork till a new release of halotools arrives.

    It would still be curious to know why not subclassing from DeprecationWarning. Could it be because certain versions of python suppresses DeprecationWarning by default? I had the impression they have eliminated this suppression in recent releases.

  5. astrofrog commented on Jan 5, 2020

    @astrofrog
    Member

    I'm not sure, though, why we are not subclassing from DeprecationWarning - we do subclass our AstropyPendingDeprecationWarning properly. Maybe @astrofrog knows?

    Historically deprecation warnings were hidden by default by Python, but we wanted to make sure users would see astropy deprecation warnings by default, hence why we don't inherit from DeprecationWarning. I think for the pending deprecation warnings we were ok with those being hidden by default. See #1871 for more information.

  6. pllim commented on Jan 5, 2020

    @pllim
    Member

    @rainwoodman , if the workaround works for you until the next release of halotools, maybe we can close this?

  7. astrofrog commented on Jan 5, 2020

    @astrofrog
    Member

    Note that we could always revise things once we support Python 3.7+, since things changed a bit there:

    https://docs.python.org/3/whatsnew/3.7.html#whatsnew37-pep565

    In particular it looks like we might want to consider inheriting from (or using directly) FutureWarning since that is to be used for warnings that should always be seen by end users.

  8. pllim commented on Jan 5, 2020

    @pllim
    Member

    @astrofrog , your suggestion at #9832 (comment) should be its own issue, no?

  9. astrofrog commented on Jan 5, 2020

    @astrofrog
    Member

    @pllim - indeed, will do

  10. mhvk commented on Jan 5, 2020

    @mhvk
    Contributor

    👍 to following PEP 565!

  11. rainwoodman commented on Jan 6, 2020

    @rainwoodman
    Author

    Yes the work around works for me. I will close this issue. Thanks!

  12. astrofrog commented on Jan 6, 2020

    @astrofrog
    Member

    Turns out @Juanlu001 had already opened an issue about PEP 565 which got closed: #8465 - if anyone feels strongly about this feel free to comment there?

  13. pllim commented on Jan 7, 2020

    @pllim
    Member

    @astrofrog , #8465 mentioned specifically python < 3.7, but what you said above was for python >= 3.7. I am now confused.

  14. astrofrog commented on Jan 7, 2020

    @astrofrog
    Member

    I'm confused too now so I need to go back and check PEP 565 :)

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