Skip to content

Commit

Permalink
ref(py): Modernize python syntax in relay (#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile-sentry authored Jun 29, 2023
1 parent 60e1d6e commit d20126d
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 135 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ clean-target-dir:
.venv/bin/pip install -U pip wheel

.venv/python-requirements-stamp: requirements-dev.txt
@# Work around https://github.com/confluentinc/confluent-kafka-python/issues/1190
@if [ "$$(uname -sm)" = "Darwin arm64" ]; then \
echo "Using 'librdkafka' from homebrew to build confluent-kafka"; \
export C_INCLUDE_PATH="$$(brew --prefix librdkafka)/include"; \
export LDFLAGS="-L$$(brew --prefix librdkafka)/lib"; \
fi; \
.venv/bin/pip install -U -r requirements-dev.txt
# Bump the mtime of an empty file.
# Make will re-run 'pip install' if the mtime on requirements-dev.txt is higher again.
Expand Down
3 changes: 3 additions & 0 deletions py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## Unreleased

This release requires Python 3.8 or later.

- Add the configuration protocol for generic metrics extraction. ([#2252](https://github.com/getsentry/relay/pull/2252))
- Modernize python syntax. ([#2264](https://github.com/getsentry/relay/pull/2264))

## 0.8.27

Expand Down
36 changes: 0 additions & 36 deletions py/sentry_relay/_compat.py

This file was deleted.

10 changes: 3 additions & 7 deletions py/sentry_relay/auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import uuid
from sentry_relay._lowlevel import lib
from sentry_relay._compat import PY2, text_type, implements_to_string
from sentry_relay.utils import (
RustObject,
encode_str,
Expand All @@ -23,7 +22,6 @@
]


@implements_to_string
class PublicKey(RustObject):
__dealloc_func__ = lib.relay_publickey_free

Expand All @@ -49,7 +47,7 @@ def __str__(self):
return decode_str(self._methodcall(lib.relay_publickey_to_string), free=True)

def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, text_type(self))
return f"<{self.__class__.__name__} {str(self)!r}>"


class SecretKey(RustObject):
Expand All @@ -66,16 +64,14 @@ def sign(self, value):
return decode_str(self._methodcall(lib.relay_secretkey_sign, buf), free=True)

def pack(self, data):
packed = json.dumps(data, separators=(",", ":"))
if not PY2:
packed = packed.encode("utf8")
packed = json.dumps(data, separators=(",", ":")).encode("utf8")
return packed, self.sign(packed)

def __str__(self):
return decode_str(self._methodcall(lib.relay_secretkey_to_string), free=True)

def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, text_type(self))
return f"<{self.__class__.__name__} {str(self)!r}>"


def generate_key_pair():
Expand Down
4 changes: 1 addition & 3 deletions py/sentry_relay/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from sentry_relay._compat import implements_to_string
from sentry_relay._lowlevel import lib


__all__ = ["RelayError"]
exceptions_by_code = {}


@implements_to_string
class RelayError(Exception):
code = None

Expand All @@ -18,7 +16,7 @@ def __init__(self, msg):
def __str__(self):
rv = self.message
if self.rust_info is not None:
return "%s\n\n%s" % (rv, self.rust_info)
return f"{rv}\n\n{self.rust_info}"
return rv


Expand Down
31 changes: 15 additions & 16 deletions py/sentry_relay/processing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json

from sentry_relay._compat import string_types, iteritems, text_type
from sentry_relay._lowlevel import lib, ffi
from sentry_relay.utils import (
encode_str,
Expand Down Expand Up @@ -65,10 +64,10 @@ def meta_with_chunks(data, meta):
return meta

result = {}
for key, item in iteritems(meta):
for key, item in meta.items():
if key == "" and isinstance(item, dict):
result[""] = item.copy()
if item.get("rem") and isinstance(data, string_types):
if item.get("rem") and isinstance(data, str):
result[""]["chunks"] = split_chunks(data, item["rem"])
elif isinstance(data, dict):
result[key] = meta_with_chunks(data.get(key), item)
Expand All @@ -88,14 +87,14 @@ class GeoIpLookup(RustObject):

@classmethod
def from_path(cls, path):
if isinstance(path, text_type):
if isinstance(path, str):
path = path.encode("utf-8")
rv = cls._from_objptr(rustcall(lib.relay_geoip_lookup_new, path))
rv._path = path
return rv

def __repr__(self):
return "<GeoIpLookup %r>" % (self._path,)
return f"<GeoIpLookup {self._path!r}>"


class StoreNormalizer(RustObject):
Expand Down Expand Up @@ -124,7 +123,7 @@ def normalize_event(self, event=None, raw_event=None):

def _serialize_event(event):
raw_event = json.dumps(event, ensure_ascii=False)
if isinstance(raw_event, text_type):
if isinstance(raw_event, str):
raw_event = raw_event.encode("utf-8", errors="replace")
return raw_event

Expand Down Expand Up @@ -157,13 +156,13 @@ def is_glob_match(
if allow_newline:
flags |= lib.GLOB_FLAGS_ALLOW_NEWLINE

if isinstance(value, text_type):
if isinstance(value, str):
value = value.encode("utf-8")
return rustcall(lib.relay_is_glob_match, make_buf(value), encode_str(pat), flags)


def is_codeowners_path_match(value, pattern):
if isinstance(value, text_type):
if isinstance(value, str):
value = value.encode("utf-8")
return rustcall(
lib.relay_is_codeowners_path_match, make_buf(value), encode_str(pattern)
Expand All @@ -178,7 +177,7 @@ def validate_pii_config(config):
as a string such that line numbers from the error message match with what
the user typed in.
"""
assert isinstance(config, string_types)
assert isinstance(config, str)
raw_error = rustcall(lib.relay_validate_pii_config, encode_str(config))
error = decode_str(raw_error, free=True)
if error:
Expand Down Expand Up @@ -232,7 +231,7 @@ def validate_sampling_condition(condition):
Validate a dynamic rule condition. Used in dynamic sampling serializer.
The parameter is a string containing the rule condition as JSON.
"""
assert isinstance(condition, string_types)
assert isinstance(condition, str)
raw_error = rustcall(lib.relay_validate_sampling_condition, encode_str(condition))
error = decode_str(raw_error, free=True)
if error:
Expand All @@ -244,7 +243,7 @@ def validate_sampling_configuration(condition):
Validate the whole sampling configuration. Used in dynamic sampling serializer.
The parameter is a string containing the rules configuration as JSON.
"""
assert isinstance(condition, string_types)
assert isinstance(condition, str)
raw_error = rustcall(
lib.relay_validate_sampling_configuration, encode_str(condition)
)
Expand All @@ -258,7 +257,7 @@ def validate_project_config(config, strict: bool):
:param strict: Whether or not to check for unknown fields.
"""
assert isinstance(config, string_types)
assert isinstance(config, str)
raw_error = rustcall(lib.relay_validate_project_config, encode_str(config), strict)
error = decode_str(raw_error, free=True)
if error:
Expand All @@ -269,10 +268,10 @@ def run_dynamic_sampling(sampling_config, root_sampling_config, dsc, event):
"""
Runs dynamic sampling on an event and returns the merged rules together with the sample rate.
"""
assert isinstance(sampling_config, string_types)
assert isinstance(root_sampling_config, string_types)
assert isinstance(dsc, string_types)
assert isinstance(event, string_types)
assert isinstance(sampling_config, str)
assert isinstance(root_sampling_config, str)
assert isinstance(dsc, str)
assert isinstance(event, str)

result_json = rustcall(
lib.run_dynamic_sampling,
Expand Down
5 changes: 2 additions & 3 deletions py/sentry_relay/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import uuid
import weakref
from sentry_relay._lowlevel import ffi, lib
from sentry_relay._compat import text_type, with_metaclass
from sentry_relay.exceptions import exceptions_by_code, RelayError


Expand Down Expand Up @@ -35,7 +34,7 @@ def rustcall(func, *args):
raise exc


class RustObject(with_metaclass(_NoDict)):
class RustObject(metaclass=_NoDict):
__slots__ = ["_objptr", "_shared"]
__dealloc_func__ = None

Expand Down Expand Up @@ -85,7 +84,7 @@ def decode_str(s, free=False):
def encode_str(s, mutable=False):
"""Encodes a RelayStr"""
rv = ffi.new("RelayStr *")
if isinstance(s, text_type):
if isinstance(s, str):
s = s.encode("utf-8")
if mutable:
s = bytearray(s)
Expand Down
5 changes: 3 additions & 2 deletions py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def build_native(spec):
def delete_scratchpad():
try:
shutil.rmtree(scratchpad)
except (IOError, OSError):
except OSError:
pass

zf = zipfile.ZipFile("rustsrc.zip")
Expand All @@ -79,7 +79,7 @@ def delete_scratchpad():
def find_dylib():
cargo_target = os.environ.get("CARGO_BUILD_TARGET")
if cargo_target:
in_path = "target/%s/%s" % (cargo_target, target)
in_path = f"target/{cargo_target}/{target}"
else:
in_path = "target/%s" % target
return build.find_dylib("relay_cabi", in_path=in_path)
Expand Down Expand Up @@ -109,6 +109,7 @@ def find_dylib():
include_package_data=True,
zip_safe=False,
platforms="any",
python_requires=">=3.8",
install_requires=['enum34>=1.1.6,<1.2.0;python_version<"3.4"', "milksnake>=0.1.2"],
setup_requires=["milksnake>=0.1.2"],
milksnake_tasks=[build_native],
Expand Down
6 changes: 1 addition & 5 deletions py/tests/test_processing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# coding: utf-8
import json
import sentry_relay

import pytest

from sentry_relay._compat import PY2

REMARKS = [["myrule", "s", 7, 17]]
META = {"": {"rem": REMARKS}}
TEXT = "Hello, [redacted]!"
Expand Down Expand Up @@ -98,8 +95,7 @@ def test_broken_json():
event = normalizer.normalize_event({"message": bad_str})
assert "Hello" in event["logentry"]["formatted"]
assert "World" in event["logentry"]["formatted"]
if not PY2:
assert event["logentry"]["formatted"] != bad_str
assert event["logentry"]["formatted"] != bad_str


@pytest.mark.parametrize(
Expand Down
1 change: 0 additions & 1 deletion py/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
import pytest

from sentry_relay._lowlevel import lib
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def config_dir(tmpdir):
def inner(name):
counters.setdefault(name, 0)
counters[name] += 1
return tmpdir.mkdir("{}-{}".format(name, counters[name]))
return tmpdir.mkdir(f"{name}-{counters[name]}")

return inner

Expand Down Expand Up @@ -156,7 +156,7 @@ def _fixture_file_path_for_test_file(test_file_path, file_name):
return path.abspath(path.join(prefix, "fixtures", test_file_name, file_name))


class _JsonFixtureProvider(object):
class _JsonFixtureProvider:
def __init__(self, test_file_path: str):
"""
Initializes a JsonFixtureProvider with the current test file path (in order to create
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
session = requests.session()


class SentryLike(object):
class SentryLike:
_health_check_passed = False

default_dsn_public_key = "31a5a894b4524f74a9a8d0e27e21ba91"
Expand Down Expand Up @@ -103,7 +103,7 @@ def wait_relay_health_check(self):
self._health_check_passed = True

def __repr__(self):
return "<{}({})>".format(self.__class__.__name__, repr(self.upstream))
return f"<{self.__class__.__name__}({repr(self.upstream)})>"

def iter_public_keys(self):
if self.public_key is not None:
Expand Down Expand Up @@ -333,7 +333,7 @@ def send_attachments(self, project_id, event_id, files, dsn_key_idx=0):
name: (file_name, file_content) for (name, file_name, file_content) in files
}
response = self.post(
"/api/{}/events/{}/attachments/".format(project_id, event_id),
f"/api/{project_id}/events/{event_id}/attachments/",
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/fixtures/gobetween.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Gobetween(SentryLike):
def __init__(self, server_address, process, upstream):
super(Gobetween, self).__init__(server_address, upstream)
super().__init__(server_address, upstream)
self.process = process


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/fixtures/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HAProxy(SentryLike):
def __init__(self, server_address, process, upstream):
super(HAProxy, self).__init__(server_address, upstream)
super().__init__(server_address, upstream)
self.process = process


Expand Down
4 changes: 2 additions & 2 deletions tests/integration/fixtures/mini_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _parse_version(version):

class Sentry(SentryLike):
def __init__(self, server_address, app):
super(Sentry, self).__init__(server_address)
super().__init__(server_address)

self.app = app
self.project_configs = {}
Expand Down Expand Up @@ -60,7 +60,7 @@ def hit(self, path):
def format_failures(self):
s = ""
for route, error in self.test_failures:
s += "> %s: %s\n" % (route, error)
s += f"> {route}: {error}\n"
return s

def add_dsn_key_to_project(self, project_id, dsn_public_key=None, numeric_id=None):
Expand Down
Loading

0 comments on commit d20126d

Please sign in to comment.