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

Change the scope of warning settings to avoid import time side effects #290

Closed
freeman-lab opened this issue Oct 2, 2020 · 3 comments · Fixed by #305
Closed

Change the scope of warning settings to avoid import time side effects #290

freeman-lab opened this issue Oct 2, 2020 · 3 comments · Fixed by #305
Labels
enhancement Idea or request for a new feature

Comments

@freeman-lab
Copy link

Hi team! Super cool project here. We're excited to be using it, and so far it's working great.

One thing we noticed is that the warnings filter set in vector.py and spline.py and elsewhere, where you call

warnings.simplefilter("default")

is specified such that simply by calling import verde anywhere else overrides existing warning filters with this particular, extremely broad setting.

As a simple example, in a fresh Python 3.7.6 shell I can call

import pickle
pickle.dump('test', open('test.pkl', 'wb'))

and I see no warning, but if I call

import verde
import pickle
pickle.dump('test', open('test.pkl', 'wb'))

I see

/usr/local/bin/ipython:1: ResourceWarning: unclosed file <_io.BufferedWriter name='test.pkl'>
  #!/usr/local/opt/python/bin/python3.7
ResourceWarning: Enable tracemalloc to get the object allocation traceback

In practice, this means we need to manually change a bunch of warning settings in a downstream package simply because we are importing verde!

Any chance you might be open to placing your warning settings inside a context manager? Something like this:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter("default")
    <any code that triggers the warnings you want to hide>

That would prevent the settings in verde from affecting other packages. If you're interested in going this route, happy to help clarify how to do it.

To see this behavior I'm running Python 3.7.6 via homebrew, OS 10.5.5, and verde 1.5.0.

@welcome
Copy link

welcome bot commented Oct 2, 2020

👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible.

You might also want to take a look at our Contributing Guide and Code of Conduct.

@santisoler
Copy link
Member

Hi @freeman-lab ! Thanks a lot for raising this issue.
I agree with you, it's not a good practice for a single library to override such global configuration on import that might affect the behaviour of any other library.

After a quick view I saw that we are only changing the warnings simplefilter to show DeprecationWarnings, so we should make the modifications you propose on those files.

Would you like to open a PR to solve this?

@leouieda
Copy link
Member

leouieda commented Oct 3, 2020

Sorry, that was my fault 🙁 I did that because deprecation warnings weren't being shown by default (which is strange to me since it makes them kind of useless).

@freeman-lab to clarify, you're suggesting setting the filter temporarily whenever we issue a warning instead of doing it at the module level? I agree with @santisoler that this something we should do and would be happy if this is something you'd like to take on.

@leouieda leouieda changed the title consider changing scope for warning settings? Change the scope of warning settings to avoid import time side effects Oct 3, 2020
@leouieda leouieda added the enhancement Idea or request for a new feature label Oct 3, 2020
leouieda pushed a commit that referenced this issue Mar 16, 2021
`DeprecationWarning`s are intended for developers, while the warnings 
we want to raise are to let users know that a feature will be deprecated 
is the `FutureWarning`. Remove the override of `warning.simplefilter`.
Following the comments in #293

Fixes #290
jessepisel added a commit to jessepisel/verde that referenced this issue Apr 13, 2021
* Fix typo on "coordinates" (fatiando#306)

Fix a common typo on Verde: replace "coordiantes" for "coordinates".

* Get INSTALL_REQUIRES from requirements.txt (fatiando#312)

On setup.py, build the INSTALL_REQUIRES variable by reading the requirements.txt file.

* Include requirements.txt in MANIFEST.in (fatiando#313)

* Exclude Dask 2021.03.0 as a dependency (fatiando#311)

Dask 2021.03.0 was causing the tests to fail under Python 3.8 on any OS.
The problem seems to be originated by dask.distributed.
By excluding this version, we avoid the problem in the future.

* Replace Travis and Azure for GitHub Actions (fatiando#309)

Remove Azure and Travis configuration files.
Add Github Actions for continuous integration and deployment.
Related to fatiando/maintenance#1

* Replace versioneer for setuptools-scm (fatiando#307)

Replace `versioneer` with `setuptools_scm` for getting Semver version of
Verde. `setuptools_scm` doesn't require to store additional files to work,
it can be installed and used through `setup.py`. Remove all `versioneer`
related files and mentions. Add `setuptools_scm` to `requirements.txt` and
`environment.yml`. Replace `setup.cfg` for `.flake8`. 
Related to fatiando/maintenance#4

* Replace DeprecationWarning for FutureWarning (fatiando#305)

`DeprecationWarning`s are intended for developers, while the warnings 
we want to raise are to let users know that a feature will be deprecated 
is the `FutureWarning`. Remove the override of `warning.simplefilter`.
Following the comments in fatiando#293

Fixes fatiando#290

* Add license notice to every source file (fatiando#308)

Add a license notice to every source file and a script for automatically
check if the license is present on every file and add it if missing.

* Update files from fatiando/contributing (fatiando#314)

Changes have been made in https://github.com/fatiando/contributing to:
MAINTENANCE.md
Update the copies in this repository to match.
See fatiando/community@b78e925

* Add .eggs to .gitignore (fatiando#315)

Ignore the .eggs directory that is created after `make install`

* Allow make_xarray_grid to take horizontal coordinates as 1d arrays (fatiando#300)

Now make_xarray_grid could take 1d arrays for horizontal coordinates, such as
how xr.DataArray stores the single dimension coordinates. Add a new
non-public meshgrid_to_1d function to convert 2d meshgrids (like the ones
generated by vd.grid_coordinates) to 1d arrays containing a single coordinate
of the corresponding axis. This function checks if all elements inside the 2d
array are equal along the right axis, i.e. a vaild meshgrid.

* Add changelog entry for v1.6.0 (fatiando#316)

* Fix wrong version numbers for PyPI releases (fatiando#317)

For test releases to TestPyPI we have to edit the setuptools_scm
configuration to produce valid version numbers. But it turns out that
this breaks the number for actual releases. To fix it, only edit the
configuration on non-release builds.

* Allow make_xarray_grid to get data as None (fatiando#318)

Used to generate a `xr.Dataset` without any `data_vars` array, 
containing only the coordinates. Will be useful in some cases 
to define an `xr.Dataset` with coordinates only and add the 
`data_vars` afterwards.

* Changelog entry for v1.6.1 (fatiando#320)

Minor release to include a small fix to make_xarray_grid

* Update files from fatiando/contributing (fatiando#321)

Changes have been made in https://github.com/fatiando/contributing to:
MAINTENANCE.md
Update the copies in this repository to match.
See fatiando/community@26724ee

* Extend support for Python 3.9 (fatiando#323)

Add Python 3.9 to the CI and setup.py flags

Co-authored-by: Santiago Soler <santiago.r.soler@gmail.com>
Co-authored-by: Fatiando a Terra Bot <50936856+fatiando-bot@users.noreply.github.com>
Co-authored-by: Leonardo Uieda <leouieda@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Idea or request for a new feature
Projects
None yet
3 participants