Skip to content

Commit

Permalink
ref: add rudimentary mypy setup (#2384)
Browse files Browse the repository at this point in the history
<!-- Describe your PR here. -->

---------

Co-authored-by: Oleksandr <1931331+olksdr@users.noreply.github.com>
  • Loading branch information
asottile-sentry and olksdr authored Aug 7, 2023
1 parent b330486 commit 18b2428
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 36 deletions.
21 changes: 2 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,14 @@ jobs:
with:
python-version: 3.8

- name: Install Dependencies
run: pip install -U -r requirements-dev.txt

- name: Run Black
run: black --check py tests --exclude '\.eggs|sentry_relay/_lowlevel.*'

- name: Run Flake8
run: flake8 py

- name: Install Rust Toolchain
run: rustup toolchain install stable --profile minimal --component clippy rustfmt rust-docs --no-self-update

- uses: swatinem/rust-cache@v2
with:
key: ${{ github.job }}

- name: Run Rustfmt
run: cargo fmt --all -- --check

- name: Run Clippy
run: cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings
- run: make style lint

- name: Check Docs
run: cargo doc --workspace --all-features --no-deps --document-private-items
Expand Down Expand Up @@ -397,11 +384,7 @@ jobs:
with:
python-version: 3.8

- name: Install dependencies
run: pip install -U -r requirements-dev.txt

- name: Run tests
run: pytest tests -n auto -v
- run: make test-integration
env:
RELAY_VERSION_CHAIN: "20.6.0,latest"

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ test-rust-all: setup-git ## run tests for Rust code with all the features enable
.PHONY: test-rust-all

test-python: setup-git setup-venv ## run tests for Python code
RELAY_DEBUG=1 .venv/bin/pip install -v --editable py
.venv/bin/pytest -v py
.PHONY: test-python

Expand Down Expand Up @@ -110,6 +109,7 @@ lint-rust: setup-git ## run lint on Rust code using clippy

lint-python: setup-venv ## run lint on Python code using flake8
.venv/bin/flake8 py tests
.venv/bin/mypy py tests
.PHONY: lint-python

lint-rust-beta: setup-git ## run lint on Rust using clippy and beta toolchain
Expand Down Expand Up @@ -166,6 +166,7 @@ clean-target-dir:

.venv/python-requirements-stamp: requirements-dev.txt
.venv/bin/pip install -U -r requirements-dev.txt
RELAY_DEBUG=1 .venv/bin/pip install -v --editable py
# Bump the mtime of an empty file.
# Make will re-run 'pip install' if the mtime on requirements-dev.txt is higher again.
touch .venv/python-requirements-stamp
Expand Down
4 changes: 4 additions & 0 deletions py/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Add rudimentary Mypy setup. ([#2384](https://github.com/getsentry/relay/pull/2384))

## 0.8.28

This release requires Python 3.8 or later.
Expand Down
7 changes: 7 additions & 0 deletions py/sentry_relay/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from sentry_relay._lowlevel import lib


Expand Down Expand Up @@ -57,3 +59,8 @@ def _make_exceptions():


_make_exceptions()

if TYPE_CHECKING:
# treat unknown attribute names as exception types
def __getattr__(name: str) -> type[RelayError]:
...
13 changes: 5 additions & 8 deletions py/sentry_relay/processing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

from sentry_relay._lowlevel import lib, ffi
Expand Down Expand Up @@ -31,23 +33,18 @@
]


VALID_PLATFORMS = frozenset()


def _init_valid_platforms():
global VALID_PLATFORMS

def _init_valid_platforms() -> frozenset[str]:
size_out = ffi.new("uintptr_t *")
strings = rustcall(lib.relay_valid_platforms, size_out)

valid_platforms = []
for i in range(int(size_out[0])):
valid_platforms.append(decode_str(strings[i], free=True))

VALID_PLATFORMS = frozenset(valid_platforms)
return frozenset(valid_platforms)


_init_valid_platforms()
VALID_PLATFORMS = _init_valid_platforms()


def split_chunks(string, remarks):
Expand Down
Empty file added py/sentry_relay/py.typed
Empty file.
3 changes: 3 additions & 0 deletions py/sentry_relay/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import os
import uuid
import weakref
from sentry_relay._lowlevel import ffi, lib
from sentry_relay.exceptions import exceptions_by_code, RelayError


attached_refs: weakref.WeakKeyDictionary[object, bytes]
attached_refs = weakref.WeakKeyDictionary()


Expand Down
9 changes: 6 additions & 3 deletions py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

DEBUG_BUILD = os.environ.get("RELAY_DEBUG") == "1"

with open("README", "rb") as f:
with open("README", encoding="UTF-8") as f:
readme = f.read()


if os.path.isfile("../relay-cabi/Cargo.toml"):
with open("../relay-cabi/Cargo.toml") as f:
version = _version_re.search(f.read()).group(1)
match = _version_re.search(f.read())
assert match is not None
version = match[1]
else:
with open("version.txt") as f:
version = f.readline().strip()
Expand Down Expand Up @@ -107,10 +109,11 @@ def find_dylib():
description="A python library to access sentry relay functionality.",
long_description=readme,
include_package_data=True,
package_data={"sentry_relay": ["py.typed", "_lowlevel.pyi"]},
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"],
install_requires=["milksnake>=0.1.2"],
setup_requires=["milksnake>=0.1.2"],
milksnake_tasks=[build_native],
cmdclass={"sdist": CustomSDist},
Expand Down
2 changes: 1 addition & 1 deletion py/tests/test_consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sentry_relay import DataCategory, SPAN_STATUS_CODE_TO_NAME
from sentry_relay.consts import DataCategory, SPAN_STATUS_CODE_TO_NAME


def test_parse_data_category():
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ exclude = '''
| target
)/
'''

[tool.mypy]
exclude = ["^py/sentry_relay/_lowlevel__ffi.py"]

[[tool.mypy.overrides]]
module = [
"confluent_kafka.*",
"msgpack.*",
"pytest_localserver.*",
]
ignore_missing_imports = true
8 changes: 7 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ redis==4.5.4
requests==2.31.0
sentry_sdk==1.14.0
werkzeug==2.2.3
sentry-relay==0.8.15
zstandard==0.18.0

# typing things
mypy==1.4.1
types-pyyaml==6.0.12.10
types-redis==4.5.1.3
types-requests==2.30.0.0
types-setuptools==65.3.0
4 changes: 3 additions & 1 deletion tests/integration/fixtures/mini_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

_version_re = re.compile(r'(?m)^version\s*=\s*"(.*?)"\s*$')
with open(os.path.join(os.path.dirname(__file__), "../../../relay/Cargo.toml")) as f:
CURRENT_VERSION = _version_re.search(f.read()).group(1)
match = _version_re.search(f.read())
assert match is not None
CURRENT_VERSION = match[1]


def _parse_version(version):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_projectconfigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import tempfile
import os

from sentry_relay import PublicKey, SecretKey, generate_key_pair
from sentry_relay.auth import PublicKey, SecretKey, generate_key_pair

RelayInfo = namedtuple("RelayInfo", ["id", "public_key", "secret_key", "internal"])

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_publickeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from collections import namedtuple

from sentry_relay import PublicKey, SecretKey, generate_key_pair
from sentry_relay.auth import PublicKey, SecretKey, generate_key_pair

RelayInfo = namedtuple("RelayInfo", ["id", "public_key", "secret_key", "internal"])

Expand Down

0 comments on commit 18b2428

Please sign in to comment.