Skip to content

ref(settings): Use proper and modern py3 importlib - #392

Merged
BYK merged 6 commits into
masterfrom
fix-imp
Aug 9, 2019
Merged

BYK merged 6 commits into
masterfrom
fix-imp

Conversation

@BYK

@BYK BYK commented Aug 2, 2019

Copy link
Copy Markdown
Member

Using __file__ and manual path logic is deprecated as of Python 3 and this patch updates our settings implementation to follow the new best practices.

Using `__file__` and manual path logic is deprecated as of Python 3 and this patch updates our settings implementation to follow the new best practices.
@BYK
BYK requested review from manuzope, tkaemming and wedamija August 2, 2019 19:35
Comment thread snuba/settings.py Outdated
settings = 'settings_%s' % settings
if not settings.endswith('.py'):
settings = '.settings_%s' % settings
elif not settings.endswith('.py'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If a shortname starts with settings_ underscore here then we'll incorrectly end up adding .py to the end. This will fail when we call import_module later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this was already the case. I'll try to see if I can make this part of the code clearer and stricter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It was the case before, but we always expected the file to have .py in the previous implementation. Now we only want to end with .py when we're actually opening a file, as opposed to a module path.

Comment thread snuba/settings.py

settings = os.path.join(path, settings)
settings = imp.load_source('snuba.settings', settings)
if settings.startswith('/'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than having two if statements, it'd probably be easier to read if you just consolidated. Especially since the .py extension can only be used when the path starts with / now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Makes sense, will see what I can do in accordance with my previous comment.

@BYK

BYK commented Aug 8, 2019

Copy link
Copy Markdown
Member Author

I still feel like this code is too permissive and a bit unpredictable with its inputs but I'm keeping it backward-compatible for now.

Comment thread snuba/settings.py Outdated
settings_module = importlib.util.module_from_spec(settings_spec)
settings_spec.loader.exec_module(settings_module)
else:
module_format = '.%s' if settings.startswith('settings_') else '.settings_'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

else '.settings_%s'?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup, fixed. Sorry.

Comment thread snuba/settings.py Outdated
settings = 'settings_%s' % settings
if not settings.endswith('.py'):
settings = '.settings_%s' % settings
elif not settings.endswith('.py'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It was the case before, but we always expected the file to have .py in the previous implementation. Now we only want to end with .py when we're actually opening a file, as opposed to a module path.

Comment thread snuba/settings.py Outdated

settings = os.path.join(path, settings)
settings = imp.load_source('snuba.settings', settings)
if settings.starstwith('/'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

startswith (test build is throwing up errors, just saw this)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

@BYK
BYK merged commit 94665d0 into master Aug 9, 2019
@BYK
BYK deleted the fix-imp branch August 9, 2019 08:18
phacops pushed a commit that referenced this pull request Jun 10, 2026
## Bump sentry-conventions to 0.11.0

Updates `sentry-conventions` from 0.10.0 to
[0.11.0](https://github.com/getsentry/sentry-conventions/releases/tag/0.11.0).

### Changes in 0.11.0

**New Features**
- Add `session.id` attribute (#412)
- Update several attributes to latest OTel representation (#418)
- Add `db.operation.batch.size` (#407)
- Add `app.vitals.start.prewarmed` attribute (#379)
- Add and deprecate runtime context attributes (#383)
- Add Android Runtime (ART) GC and memory attributes (#382)
- Add `db.stored_procedure.name` and ensure span name attribute
consistency (#398)
- Add Cloudflare SDK attributes (#392)
- Add missing GCP and FaaS attributes (#403)

**Bug Fixes**
- Set `pii: 'maybe'` on `faas` string attributes (#415)
- Add missing Cloudflare visibility attributes (#408)

**Internal**
- Remove `sdks` field from attribute schema and definitions (#410)
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