Skip to content

Commit

Permalink
Address TODOs related to Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
iliakur committed Oct 18, 2024
1 parent 7fff4ed commit 83733aa
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 81 deletions.
3 changes: 1 addition & 2 deletions datadog_checks_dev/datadog_checks/dev/_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ def serialize_data(data):
# 1. Printing to stdout won't fail
# 2. Easy parsing since there are no spaces
#
# TODO: Remove str() when we drop Python 2
return str(urlsafe_b64encode(data.encode('utf-8')).decode('utf-8'))
return urlsafe_b64encode(data.encode('utf-8')).decode('utf-8')


def deserialize_data(data):
Expand Down
8 changes: 2 additions & 6 deletions datadog_checks_dev/datadog_checks/dev/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,10 @@ def copy_dir_contents(path, d):
def remove_path(path):
try:
shutil.rmtree(path, ignore_errors=False)
# TODO: Remove FileNotFoundError (and noqa: B014) when Python 2 is removed
# In Python 3, IOError have been merged into OSError
except (FileNotFoundError, OSError): # noqa: B014
except OSError:
try:
os.remove(path)
# TODO: Remove FileNotFoundError (and noqa: B014) when Python 2 is removed
# In Python 3, IOError have been merged into OSError
except (FileNotFoundError, OSError, PermissionError): # noqa: B014
except (OSError, PermissionError):
pass


Expand Down
6 changes: 1 addition & 5 deletions datadog_checks_dev/datadog_checks/dev/ssh_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import subprocess
from contextlib import contextmanager

import psutil

from .conditions import WaitForPortListening
from .env import environment_run
from .structures import LazyFunction, TempDir
Expand Down Expand Up @@ -141,7 +139,5 @@ def __call__(self):
with TempDir(self.temp_name) as temp_dir:
with open(os.path.join(temp_dir, self.pid_file)) as pid_file:
pid = int(pid_file.read())
# TODO: Remove psutil as a dependency when we drop Python 2, on Python 3 os.kill supports Windows
process = psutil.Process(pid)
process.kill()
os.kill(pid)
return 0
6 changes: 0 additions & 6 deletions datadog_checks_dev/datadog_checks/dev/tooling/e2e/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,6 @@ def start_agent(self):
'DD_CMD_PORT': find_free_port(get_ip()),
# Disable trace agent
'DD_APM_ENABLED': 'false',
# Don't write .pyc, needed to fix this issue (only Python 2):
# When reinstalling a package, .pyc are not cleaned correctly. The issue is fixed by not writing them
# in the first place.
# More info: https://github.com/DataDog/integrations-core/pull/5454
# TODO: Remove PYTHONDONTWRITEBYTECODE env var when Python 2 support is removed
'PYTHONDONTWRITEBYTECODE': "1",
"DD_TELEMETRY_ENABLED": "1",
}
if self.dd_site:
Expand Down
1 change: 0 additions & 1 deletion datadog_checks_dev/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dependencies = [
"coverage>=5.0.3",
"flaky>=3.8.0",
"mock",
"psutil",
"pytest==8.1.1",
"pytest-asyncio>=0.23.4",
"pytest-benchmark[histogram]>=4.0.0",
Expand Down
18 changes: 5 additions & 13 deletions ddev/src/ddev/cli/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def test(
import os
import sys

from ddev.repo.constants import PYTHON_VERSION
from ddev.testing.constants import EndToEndEnvVars, TestEnvVars
from ddev.testing.hatch import get_hatch_env_vars
from ddev.utils.ci import running_in_ci
Expand Down Expand Up @@ -238,18 +237,11 @@ def test(

if standard_tests:
if ddtrace and (target.is_integration or target.name == 'datadog_checks_base'):
# TODO: remove this once we drop Python 2
if app.platform.windows and (
(python_filter and python_filter != PYTHON_VERSION)
or not all(env_name.startswith('py3') for env_name in chosen_environments)
):
app.display_warning('Tracing is only supported on Python 3 on Windows')
else:
command.append('--ddtrace')
env_vars['DDEV_TRACE_ENABLED'] = 'true'
env_vars['DD_PROFILING_ENABLED'] = 'true'
env_vars['DD_SERVICE'] = os.environ.get('DD_SERVICE', 'ddev-integrations')
env_vars['DD_ENV'] = os.environ.get('DD_ENV', 'ddev-integrations')
command.append('--ddtrace')
env_vars['DDEV_TRACE_ENABLED'] = 'true'
env_vars['DD_PROFILING_ENABLED'] = 'true'
env_vars['DD_SERVICE'] = os.environ.get('DD_SERVICE', 'ddev-integrations')
env_vars['DD_ENV'] = os.environ.get('DD_ENV', 'ddev-integrations')

if junit:
# In order to handle multiple environments the report files must contain the environment name.
Expand Down
8 changes: 0 additions & 8 deletions ddev/src/ddev/e2e/agent/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ def start(self, *, agent_build: str, local_packages: dict[Path, str], env_vars:
env_vars[AgentEnvVars.TELEMETRY_ENABLED] = '1'
env_vars[AgentEnvVars.EXPVAR_PORT] = '5000'

# TODO: Remove this when Python 2 support is removed
#
# Don't write .pyc, needed to fix this issue (only Python 2):
# More info: https://github.com/DataDog/integrations-core/pull/5454
# When reinstalling a package, .pyc are not cleaned correctly. The issue is fixed by not writing them
# in the first place.
env_vars['PYTHONDONTWRITEBYTECODE'] = '1'

if (proxy_data := self.metadata.get('proxy')) is not None:
if (http_proxy := proxy_data.get('http')) is not None:
env_vars[AgentEnvVars.PROXY_HTTP] = http_proxy
Expand Down
8 changes: 2 additions & 6 deletions ddev/src/ddev/e2e/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def stop(self) -> Generator[list[str], None, None]:
yield self._base_command()

def _base_command(self) -> list[str]:
command = [
return [
sys.executable,
'-m',
'hatch',
Expand All @@ -43,9 +43,5 @@ def _base_command(self) -> list[str]:
'--exitfirst',
# We need -2 verbosity and by default the test command sets the verbosity to +2
'-qqqq',
'--no-header',
]
# TODO: always use this flag when we drop support for Python 2
if not self.__env.startswith('py2'):
command.append('--no-header')

return command
15 changes: 1 addition & 14 deletions ddev/src/ddev/plugin/external/hatch/environment_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def check_types(self):

@cached_property
def mypy_args(self):
return self.config.get('mypy-args', [])
return self.config.get('mypy-args', []) + ['--install-types', '--non-interactive']

@cached_property
def mypy_deps(self):
Expand Down Expand Up @@ -157,19 +157,6 @@ def get_initial_config(self):
f'mypy --config-file=../pyproject.toml {" ".join(self.mypy_args)}'.rstrip()
]
lint_env['scripts']['all'].append('typing')
lint_env['dependencies'].extend(
[
# TODO: remove extra when we drop Python 2
'mypy[python2]==0.910; python_version<"3"',
'mypy[python2]==1.3.0; python_version>"3"',
# TODO: remove these when drop Python 2 and replace with --install-types --non-interactive
'types-python-dateutil==2.8.2',
'types-pyyaml==5.4.10',
'types-requests==2.25.11',
'types-simplejson==3.17.5',
'types-six==1.16.2',
]
)
lint_env['dependencies'].extend(self.mypy_deps)

return config
3 changes: 1 addition & 2 deletions ibm_mq/datadog_checks/ibm_mq/process_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def join_command_args(command_args):
import shlex

def join_command_args(command_args):
# TODO: when we drop Python 2 use `shlex.join`
return ' '.join(shlex.quote(arg) for arg in command_args)
return shlex.join(shlex.quote(arg) for arg in command_args)


class QueueManagerProcessMatcher(ConditionLimiter):
Expand Down
16 changes: 1 addition & 15 deletions redisdb/datadog_checks/redisdb/redisdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,7 @@ def upstream_parse_slowlog_get(response, **options):
max_slow_entries = int(self.instance.get(MAX_SLOW_ENTRIES_KEY))

# Get all slowlog entries
try:
slowlogs = conn.slowlog_get(max_slow_entries)
except TypeError as e:
# This catch is needed in PY2 because there is a known issue that has only been fixed after redis
# dropped python 2 support
# issue: https://github.com/andymccurdy/redis-py/issues/1475
# fix: https://github.com/andymccurdy/redis-py/pull/1352
# TODO: remove once PY2 is no longer supported
self.log.exception(e)
self.log.error(
'There was an error retrieving slowlog, these metrics will be skipped. This issue is fixed on Agent 7+.'
' You can find more information about upgrading to agent 7 in '
'https://docs.datadoghq.com/agent/versions/upgrade_to_agent_v7/?tab=linux'
)
slowlogs = []
slowlogs = conn.slowlog_get(max_slow_entries)

# Find slowlog entries between last timestamp and now using start_time
slowlogs = [s for s in slowlogs if s['start_time'] > self.last_timestamp_seen]
Expand Down
4 changes: 1 addition & 3 deletions teamcity/datadog_checks/teamcity/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
import re
import time
from collections import OrderedDict
from copy import deepcopy
from urllib.parse import urlparse

Expand Down Expand Up @@ -46,8 +45,7 @@ def filter_items(items, key, default_limit, default_include, default_exclude, co
include_patterns = config_key.get('include', default_include)
exclude_patterns = config_key.get('exclude', default_exclude)
filtered_items = filter_list(items, include_patterns, exclude_patterns)
# TODO: Use plain dict when we drop Python 2 support.
ordered_items = OrderedDict(list(filtered_items.items())[:limit])
ordered_items = dict(list(filtered_items.items())[:limit])
reached_limit = len(ordered_items) < len(filtered_items)
return ordered_items, reached_limit

Expand Down

0 comments on commit 83733aa

Please sign in to comment.