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

fix: clear unwanted global variables in module init #3095

Closed
wants to merge 4 commits into from

Conversation

gen-xu
Copy link
Contributor

@gen-xu gen-xu commented Dec 19, 2022

Description

I found this by trying to use importlib.reload.

import importlib
import opentelemetry.sdk.metrics as metrics
importlib.reload(metrics)

will raise error like this

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/gen/.pyenv/versions/3.11.1/lib/python3.11/importlib/__init__.py", line 169, in reload
    _bootstrap._exec(spec, module)
  File "<frozen importlib._bootstrap>", line 621, in _exec
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/gen/.pyenv/versions/3.11.1/lib/python3.11/site-packages/opentelemetry/sdk/metrics/__init__.py", line 36, in <module>
    value.__module__ = __name__
    ^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '__module__'

The str above is basically the key in the previous run of this loop.

Though I understand that reload isn't any guaranteed behavior, but without del key, value it polluted the module level globals, that I think is unexected.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

import importlib
import opentelemetry.sdk.metrics as metrics
importlib.reload(metrics)

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@gen-xu gen-xu requested a review from a team December 19, 2022 12:47
@gen-xu gen-xu changed the title fix: clear key value in module level fix: clear unwanted global variables in module init Dec 19, 2022
@gen-xu gen-xu force-pushed the gen/del_key_value_on_init branch 2 times, most recently from 7a05966 to 015c9e1 Compare January 3, 2023 06:10
@gen-xu gen-xu force-pushed the gen/del_key_value_on_init branch from 015c9e1 to 7b6e58b Compare January 3, 2023 06:10
@shalevr
Copy link
Member

shalevr commented Feb 8, 2023

Is it possible to use private variables instead of deleting them?
Like this:
for __key, __value in globals().copy().items():

@ocelotl
Copy link
Contributor

ocelotl commented Mar 14, 2023

We have reverted to a static to define __all__ (see #3143), thanks for this PR, though 🙂

@ocelotl ocelotl closed this Mar 14, 2023
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.

4 participants