Skip to content

Remove python 2 support #78

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

Merged
merged 5 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pipeline {
axes {
axis {
name 'VERSION'
values '2.7', '3.6', '3.7', '3.8'
values '3.6', '3.7', '3.8', '3.9', '3.10'
}
}
stages {
Expand Down Expand Up @@ -88,7 +88,7 @@ pipeline {
}

def python(Map v = [:], body) {
def dockerImage = v.version.equals('2.7') ? 'python:3.7' : "python:${v.version}"
def dockerImage = "python:${v.version}"
// If dockerhub got issues then let's retry twice
retry(2) {
sleep 5
Expand Down
11 changes: 3 additions & 8 deletions ecs_logging/_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,9 @@ def __init__(
if validate is not None:
# validate was introduced in py3.8 so we need to only provide it if the user provided it
_kwargs["validate"] = validate
if sys.version_info < (3, 0): # Different args in py2.7
super(StdlibFormatter, self).__init__( # type: ignore[call-arg]
fmt=fmt, datefmt=datefmt
)
else:
super(StdlibFormatter, self).__init__( # type: ignore[call-arg]
fmt=fmt, datefmt=datefmt, style=style, **_kwargs
)
super().__init__( # type: ignore[call-arg]
fmt=fmt, datefmt=datefmt, style=style, **_kwargs
)

if stack_trace_limit is not None:
if not isinstance(stack_trace_limit, int):
Expand Down
19 changes: 5 additions & 14 deletions ecs_logging/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,15 @@ def flatten_dict(value):
for key, val in value.items():
if not isinstance(val, collections_abc.Mapping):
if key in top_level:
raise ValueError(
"Duplicate entry for '%s' with different nesting" % key
)
raise ValueError(f"Duplicate entry for '{key}' with different nesting")
top_level[key] = val
else:
val = flatten_dict(val)
for vkey, vval in val.items():
vkey = "%s.%s" % (key, vkey)
vkey = f"{key}.{vkey}"
if vkey in top_level:
raise ValueError(
"Duplicate entry for '%s' with different nesting" % vkey
f"Duplicate entry for '{vkey}' with different nesting"
)
top_level[vkey] = vval

Expand Down Expand Up @@ -163,16 +161,9 @@ def json_dumps(value):
# Need to call json.dumps() on values just in
# case the given values aren't strings (even though
# they should be according to the spec)
ordered_json = ",".join(
'"%s":%s'
% (
k,
json_dumps(v),
)
for k, v in ordered_fields
)
ordered_json = ",".join(f'"{k}":{json_dumps(v)}' for k, v in ordered_fields)
if value:
return "{%s,%s" % (
return "{{{},{}".format(
ordered_json,
json_dumps(value)[1:],
)
Expand Down
8 changes: 4 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ def tests_impl(session):
"--junitxml=junit-test.xml",
"--cov=ecs_logging",
*(session.posargs or ("tests/",)),
env={"PYTHONWARNINGS": "always::DeprecationWarning"}
env={"PYTHONWARNINGS": "always::DeprecationWarning"},
)


@nox.session(python=["2.7", "3.6", "3.7", "3.8"])
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"])
def test(session):
tests_impl(session)


@nox.session()
def blacken(session):
session.install("black")
session.run("black", "--target-version=py27", *SOURCE_FILES)
session.run("black", "--target-version=py36", *SOURCE_FILES)

lint(session)


@nox.session
def lint(session):
session.install("flake8", "black", "mypy")
session.run("black", "--check", "--target-version=py27", *SOURCE_FILES)
session.run("black", "--check", "--target-version=py36", *SOURCE_FILES)
session.run("flake8", "--ignore=E501,W503", *SOURCE_FILES)
session.run(
"mypy",
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
Expand All @@ -26,7 +25,7 @@ classifiers = [
requires = [
"backports.functools-lru-cache; python_version < '3.3'"
]
requires-python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*"
requires-python = ">=3.6"

[tool.flit.metadata.requires-extra]
develop = [
Expand Down
25 changes: 0 additions & 25 deletions tests/compat.py

This file was deleted.

28 changes: 8 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ class ValidationError(Exception):
pass


if sys.version_info[0] >= 3:
basestring = str


@pytest.fixture
def spec_validator():
with open(
os.path.join(os.path.dirname(__file__), "resources", "spec.json"), "r"
) as fh:
with open(os.path.join(os.path.dirname(__file__), "resources", "spec.json")) as fh:
spec = json.load(fh)

def validator(data_json):
Expand All @@ -63,13 +57,11 @@ def validator(data_json):
if subval:
found = True
if not found:
raise ValidationError("Missing required key {}".format(k))
raise ValidationError(f"Missing required key {k}")
if k in data:
if v["type"] == "string" and not (
isinstance(data[k], str) or isinstance(data[k], basestring)
):
if v["type"] == "string" and not isinstance(data[k], str):
raise ValidationError(
"Value {0} for key {1} should be string, is {2}".format(
"Value {} for key {} should be string, is {}".format(
data[k], k, type(data[k])
)
)
Expand All @@ -78,12 +70,12 @@ def validator(data_json):
datetime.datetime.strptime(data[k], "%Y-%m-%dT%H:%M:%S.%fZ")
except ValueError:
raise ValidationError(
"Value {0} for key {1} doesn't parse as an ISO datetime".format(
"Value {} for key {} doesn't parse as an ISO datetime".format(
data[k], k
)
)
if v.get("index") and list(data.keys())[v.get("index")] != k:
raise ValidationError("Key {0} is not at index {1}".format(k, index))
raise ValidationError(f"Key {k} is not at index {index}")

return data_json

Expand All @@ -92,12 +84,8 @@ def validator(data_json):

@pytest.fixture
def apm():
if sys.version_info < (3, 6):
pytest.skip("elasticapm only supports python 3.6+")
if sys.version_info[0] >= 3:
record_factory = logging.getLogRecordFactory()
record_factory = logging.getLogRecordFactory()
apm = elasticapm.Client({"SERVICE_NAME": "apm-service", "DISABLE_SEND": True})
yield apm
apm.close()
if sys.version_info[0] >= 3:
logging.setLogRecordFactory(record_factory)
logging.setLogRecordFactory(record_factory)
2 changes: 1 addition & 1 deletion tests/test_apm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging
import structlog
import pytest
from .compat import StringIO
from io import StringIO


def test_elasticapm_structlog_log_correlation_ecs_fields(spec_validator, apm):
Expand Down
14 changes: 4 additions & 10 deletions tests/test_stdlib_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@

import logging
import logging.config
import mock
from unittest import mock
import pytest
import json
import time
import random
import sys
import ecs_logging
from .compat import StringIO

requires_py3 = pytest.mark.skipif(
sys.version_info[0] < 3, reason="Test requires Python 3.x+"
)
from io import StringIO


@pytest.fixture(scope="function")
def logger():
return logging.getLogger("test-logger-%f-%f" % (time.time(), random.random()))
return logging.getLogger(f"test-logger-{time.time():f}-{random.random():f}")


def make_record():
Expand Down Expand Up @@ -78,7 +74,7 @@ def test_extra_global_is_merged(spec_validator):
def test_can_be_overridden(spec_validator):
class CustomFormatter(ecs_logging.StdlibFormatter):
def format_to_ecs(self, record):
ecs_dict = super(CustomFormatter, self).format_to_ecs(record)
ecs_dict = super().format_to_ecs(record)
ecs_dict["custom"] = "field"
return ecs_dict

Expand Down Expand Up @@ -311,7 +307,6 @@ def test_exclude_fields_type_and_values():
assert str(e.value) == "'exclude_fields' must be a sequence of strings"


@requires_py3
def test_stack_info(logger):
stream = StringIO()
handler = logging.StreamHandler(stream)
Expand All @@ -327,7 +322,6 @@ def test_stack_info(logger):
assert "test_stack_info" in error_stack_trace and __file__ in error_stack_trace


@requires_py3
@pytest.mark.parametrize("exclude_fields", [["error"], ["error.stack_trace"]])
def test_stack_info_excluded(logger, exclude_fields):
stream = StringIO()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_structlog_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import ecs_logging
import structlog
import mock
from .compat import StringIO
from unittest import mock
from io import StringIO

import pytest

Expand Down