Skip to content

Commit

Permalink
replace flake8 by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Brulatout committed Aug 23, 2023
1 parent 1e1bf5d commit 4c49466
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 89 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ default_stages: [commit]
repos:
- repo: local
hooks:
- id: ruff
name: ruff
entry: ruff check
language: system
types_or: [python]

- id: black
name: black formatter
Expand Down
54 changes: 22 additions & 32 deletions consul/base.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class BadRequest(ConsulException):
class ClientError(ConsulException):
"""Encapsulates 4xx Http error code"""

pass


#
Expand Down Expand Up @@ -128,13 +127,18 @@ def _compat(self, script=None, interval=None, ttl=None, http=None, timeout=None,
ret = {"check": {}}

if script:
assert interval and not (ttl or http)
assert interval
assert not ttl
assert not http
ret["check"] = {"script": script, "interval": interval}
if ttl:
assert not (interval or script or http)
assert not interval or script
assert not http
ret["check"] = {"ttl": ttl}
if http:
assert interval and not (script or ttl)
assert interval
assert not script
assert not ttl
ret["check"] = {"http": http, "interval": interval}
if timeout:
assert http
Expand Down Expand Up @@ -1770,10 +1774,7 @@ def create(self, name=None, node=None, checks=None, lock_delay=15, behavior="rel
if ttl:
assert 10 <= ttl <= 86400
data["ttl"] = "%ss" % ttl
if data:
data = json.dumps(data)
else:
data = ""
data = json.dumps(data) if data else ""

return self.agent.http.put(CB.json(is_id=True), "/v1/session/create", params=params, data=data)

Expand Down Expand Up @@ -1988,10 +1989,7 @@ def create(self, name=None, type="client", rules=None, acl_id=None, token=None):
if acl_id:
payload["ID"] = acl_id

if payload:
data = json.dumps(payload)
else:
data = ""
data = json.dumps(payload) if payload else ""

return self.agent.http.put(CB.json(is_id=True), "/v1/acl/create", params=params, data=data)

Expand Down Expand Up @@ -2123,45 +2121,37 @@ def _query_data(
ttl=None,
regexp=None,
):
service_body = dict(
[
(k, v)
service_body = {
k: v
for k, v in {
"service": service,
"onlypassing": onlypassing,
"tags": tags,
"failover": dict(
[
(k, v)
"failover": {
k: v
for k, v in {"nearestn": nearestn, "datacenters": datacenters}.items()
if v is not None
]
),
},
}.items()
if v is not None
]
)
}

data = dict(
[
(k, v)
data = {
k: v
for k, v in {
"name": name,
"session": session,
"token": token or self.agent.token,
"dns": {"ttl": ttl} if ttl is not None else None,
"template": dict(
[
(k, v)
"template": {
k: v
for k, v in {"type": "name_prefix_match", "regexp": regexp}.items()
if v is not None
]
),
},
"service": service_body,
}.items()
if v is not None
]
)
}
return json.dumps(data)

def create(
Expand Down
10 changes: 4 additions & 6 deletions consul/twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def getContext(self, hostname, port):
class HTTPClient(base.HTTPClient):
def __init__(self, contextFactory, *args, **kwargs):
super(HTTPClient, self).__init__(*args, **kwargs)
agent_kwargs = dict(reactor=reactor, pool=HTTPConnectionPool(reactor))
agent_kwargs = {"reactor": reactor, "pool": HTTPConnectionPool(reactor)}
if contextFactory is not None:
# use the provided context factory
agent_kwargs["contextFactory"] = contextFactory
Expand Down Expand Up @@ -62,12 +62,10 @@ def compat_string(value):
def _get_resp(self, response):
# Merge multiple header values as per RFC2616
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
headers = dict(
[
(self.compat_string(k), ",".join(map(self.compat_string, v)))
headers = {
self.compat_string(k): ",".join(map(self.compat_string, v))
for k, v in dict(response.headers.getAllRawHeaders()).items()
]
)
}
body = yield response.text(encoding="utf-8")
returnValue((response.code, headers, body))

Expand Down
Empty file added docs/__init__.py
Empty file.
67 changes: 66 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,69 @@ preview = true
line_length = 120
case_sensitive = true
profile = "black"
src_paths = ["."]
src_paths = ["."]

[tool.ruff]
target-version = 'py39'
# See complete list : https://beta.ruff.rs/docs/rules
select = [
# "F", # pyflakes
# "E", # pycodestyle errors
"W", # pycodestyle warnings
"C90", # mccabe
# "N", # pep8-naming
# "UP", # pyupgrade
"YTT", # flake8-2020
"CPY", # Copyright-related rules
"C4", # flake8-comprehensions
"T10", # flake8-debugger
"DJ", # flake8-django
"EXE", # flake8-executable
"FA", # flake8-future-annotations
"ISC", # flake8-implicit-str-concat
"ICN", # flake8-import-conventions
"INP", # flake8-no-pep420
"PIE", # flake8-pie
"T20", # flake8-print
# "PYI", # flake8-pyi
# "PT", # flake8-pytest-style
"Q", # flake8-quotes
"RSE", # flake8-raise
# "SLF", # flake8-self
"SLOT", # flake8-slots
# "SIM", # flake8-simplify
"TID", # flake8-tidy-imports
"TCH", # flake8-type-checking
"INT", # flake8-gettext
"PD", # pandas-vet
"PLC", # pylint-convention
"PLE", # pylint-errors
"PLW", # pylint-warnings
"FLY", # flynt
"NPY", # numpy-specific rules
"AIR", # airflow
]

fixable = [
"PIE",
"PT",
"C4",
"SIM",
"SIM",
"UP",
]

# Never enforce some rules
ignore = [
"E501", # line length violations
]

## Ignore some rules for some files
[tool.ruff.per-file-ignores]
"**/*test*.py" = [
"SLF001", # Private member accessed
"T201", # print found
]

[tool.ruff.mccabe]
max-complexity = 15
1 change: 1 addition & 0 deletions tests-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pyOpenSSL
pytest
pytest-rerunfailures
pytest-twisted
ruff
tornado
treq
twisted
Empty file added tests/__init__.py
Empty file.
9 changes: 3 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ def start_consul_instance(acl_master_token=None):
tmpdir.chdir()

(system, node, release, version, machine, processor) = platform.uname()
if system == "Darwin":
postfix = "osx"
else:
postfix = "linux64"
postfix = "osx" if system == "Darwin" else "linux64"
bin = os.path.join(os.path.dirname(__file__), "consul." + postfix)
command = "{bin} agent -dev -bind=127.0.0.1 -config-dir=."
command = command.format(bin=bin).strip()
Expand Down Expand Up @@ -107,7 +104,7 @@ def consul_instance():
p.terminate()


@pytest.fixture
@pytest.fixture()
def consul_port(consul_instance):
port = consul_instance
yield port
Expand All @@ -122,7 +119,7 @@ def acl_consul_instance():
p.terminate()


@pytest.fixture
@pytest.fixture()
def acl_consul(acl_consul_instance):
ACLConsul = collections.namedtuple("ACLConsul", ["port", "token"])
port, token = acl_consul_instance
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Check = consul.Check


@pytest.fixture
@pytest.fixture()
def loop(request):
asyncio.set_event_loop(None)
loop = asyncio.new_event_loop()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_base.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_status_200_passes(self):
CB._status(response)

@pytest.mark.parametrize(
"response, expected_exception",
("response", "expected_exception"),
[
(Response(400, None, None), consul.base.BadRequest),
(Response(401, None, None), consul.base.ACLDisabled),
Expand Down Expand Up @@ -177,7 +177,7 @@ class TestChecks:
"""

@pytest.mark.parametrize(
"url, interval, timeout, deregister, header, want",
("url", "interval", "timeout", "deregister", "header", "want"),
[
(
"http://example.com",
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_http_check(self, url, interval, timeout, deregister, header, want):
assert ch == want

@pytest.mark.parametrize(
"host, port, interval, timeout, deregister, want",
("host", "port", "interval", "timeout", "deregister", "want"),
[
(
"localhost",
Expand Down Expand Up @@ -305,7 +305,7 @@ def test_tcp_check(self, host, port, interval, timeout, deregister, want):
assert ch == want

@pytest.mark.parametrize(
"container_id, shell, script, interval, deregister, want",
("container_id", "shell", "script", "interval", "deregister", "want"),
[
(
"wandering_bose",
Expand Down
Loading

0 comments on commit 4c49466

Please sign in to comment.