Skip to content

Commit 5b65166

Browse files
authored
fix: Use pre-3.9 dict merge operator (#50)
* fix: Use pre-3.9 dict merge operator * Use dict merge syntax compatible with Python 3.8 * Rename test modules to `pyright` doesn't get confused * Fixed some `typing` details * Other misc formatting Signed-off-by: Sam Lock <sam@swlock.co.uk> * remove pointless test that requires python 3.8 to be useful Signed-off-by: Sam Lock <sam@swlock.co.uk> * changelog Signed-off-by: Sam Lock <sam@swlock.co.uk> --------- Signed-off-by: Sam Lock <sam@swlock.co.uk>
1 parent 029cef0 commit 5b65166

File tree

9 files changed

+37
-36
lines changed

9 files changed

+37
-36
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### Bug fixes
4+
5+
- Use pre-3.9 dict merge operator
6+
17
## v0.10.4 (2024-01-24)
28

39
### Bug fixes

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ You can pass additional options in the `channel_options` dict.
132132
Available options are described [here](https://github.com/grpc/grpc/blob/7536d8a849c0096e4c968e7730306872bb5ec674/include/grpc/impl/grpc_types.h).
133133
The argument is of type `dict[str, Any]` where the `Any` value must match the expected type defined in the previous link.
134134

135+
IMPORTANT: We use the config key `grpc.service_config` to set service-specific configuration (retry policies, backoffs etc) within the nested JSON field. Passing this as a `channel_options` key will override that configuration entirely. We recommend leaving this untouched, however, if you need to pass custom config, ensure you pass the entire existing dict along with the desired updates (this can be found within the `AsyncClientBase.__init__` method).
136+
135137
NOTE: We provide this as a generic method to set arbitrary options for particular use cases.
136138
For purely demonstrative purposes, our example below overrides `grpc.ssl_target_name_override`, which is certainly not recommended practice for production applications.
137139

cerbos/sdk/_async/_grpc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ssl
77
import uuid
88
from functools import wraps
9-
from typing import Any, Dict, List, Tuple, Union
9+
from typing import Any, Dict, List, Optional, Tuple, Union
1010

1111
import grpc
1212

@@ -79,8 +79,8 @@ class AsyncClientBase:
7979
def __init__(
8080
self,
8181
host: str,
82-
creds: grpc.ChannelCredentials,
83-
methods: List[Dict[str, str]] = None,
82+
creds: Optional[grpc.ChannelCredentials],
83+
methods: Optional[List[Dict[str, str]]] = None,
8484
tls_verify: TLSVerify = False,
8585
timeout_secs: Union[float, None] = None,
8686
request_retries: int = 0,
@@ -123,10 +123,10 @@ def __init__(
123123
}
124124

125125
if channel_options:
126-
options |= channel_options
126+
options = {**options, **channel_options}
127127

128128
opts = [(k, v) for k, v in options.items()]
129-
if tls_verify:
129+
if tls_verify and creds:
130130
self._channel = grpc.aio.secure_channel(
131131
host,
132132
credentials=creds,
@@ -179,7 +179,7 @@ def __init__(
179179
wait_for_ready: bool = False,
180180
channel_options: Union[Dict[str, Any], None] = None,
181181
):
182-
creds: grpc.ChannelCredentials = None
182+
creds: Optional[grpc.ChannelCredentials] = None
183183
if tls_verify:
184184
cert = get_cert(tls_verify)
185185
creds = grpc.ssl_channel_credentials(cert)
@@ -448,7 +448,7 @@ def __init__(
448448
admin_credentials = admin_credentials or AdminCredentials()
449449
self._creds_metadata = admin_credentials.metadata()
450450

451-
creds: grpc.ChannelCredentials = None
451+
creds: Optional[grpc.ChannelCredentials] = None
452452
if tls_verify:
453453
cert = get_cert(tls_verify)
454454
creds = grpc.ssl_channel_credentials(cert)

cerbos/sdk/_sync/_grpc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ssl
77
import uuid
88
from functools import wraps
9-
from typing import Any, Dict, List, Tuple, Union
9+
from typing import Any, Dict, List, Optional, Tuple, Union
1010

1111
import grpc
1212

@@ -79,8 +79,8 @@ class SyncClientBase:
7979
def __init__(
8080
self,
8181
host: str,
82-
creds: grpc.ChannelCredentials,
83-
methods: List[Dict[str, str]] = None,
82+
creds: Optional[grpc.ChannelCredentials],
83+
methods: Optional[List[Dict[str, str]]] = None,
8484
tls_verify: TLSVerify = False,
8585
timeout_secs: Union[float, None] = None,
8686
request_retries: int = 0,
@@ -123,10 +123,10 @@ def __init__(
123123
}
124124

125125
if channel_options:
126-
options |= channel_options
126+
options = {**options, **channel_options}
127127

128128
opts = [(k, v) for k, v in options.items()]
129-
if tls_verify:
129+
if tls_verify and creds:
130130
self._channel = grpc.secure_channel(
131131
host,
132132
credentials=creds,
@@ -179,7 +179,7 @@ def __init__(
179179
wait_for_ready: bool = False,
180180
channel_options: Union[Dict[str, Any], None] = None,
181181
):
182-
creds: grpc.ChannelCredentials = None
182+
creds: Optional[grpc.ChannelCredentials] = None
183183
if tls_verify:
184184
cert = get_cert(tls_verify)
185185
creds = grpc.ssl_channel_credentials(cert)
@@ -448,7 +448,7 @@ def __init__(
448448
admin_credentials = admin_credentials or AdminCredentials()
449449
self._creds_metadata = admin_credentials.metadata()
450450

451-
creds: grpc.ChannelCredentials = None
451+
creds: Optional[grpc.ChannelCredentials] = None
452452
if tls_verify:
453453
cert = get_cert(tls_verify)
454454
creds = grpc.ssl_channel_credentials(cert)

pyproject.toml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
name = "cerbos"
33
description = "SDK for working with Cerbos: an open core, language-agnostic, scalable authorization solution"
44
readme = "README.md"
5-
authors = [
6-
{name = "Cerbos Developers", email = "sdk+python@cerbos.dev"},
7-
]
8-
license = {text = "Apache-2.0"}
5+
authors = [{ name = "Cerbos Developers", email = "sdk+python@cerbos.dev" }]
6+
license = { text = "Apache-2.0" }
97
classifiers = [
108
"Development Status :: 4 - Beta",
119
"Intended Audience :: Developers",
@@ -27,25 +25,18 @@ requires-python = ">=3.8"
2725
dynamic = ["version"]
2826

2927

30-
3128
[project.urls]
3229
Homepage = "https://cerbos.dev"
3330

3431
[project.optional-dependencies]
35-
testcontainers = [
36-
"testcontainers>=3.5.3",
37-
]
32+
testcontainers = ["testcontainers>=3.5.3"]
3833

3934
[tool.pdm.version]
4035
source = "scm"
4136

4237
[tool.pdm.dev-dependencies]
43-
lint = [
44-
"black>=22.3.0",
45-
"isort>=5.10.1"]
46-
test = [
47-
"pytest>=7.3.1",
48-
]
38+
lint = ["black>=22.3.0", "isort>=5.10.1"]
39+
test = ["pytest>=7.3.1"]
4940
tools = [
5041
"unasync>=0.5.0",
5142
"setuptools>=63.2.0",
@@ -59,13 +50,13 @@ tools = [
5950
includes = ["cerbos/", "google/", "buf/"]
6051

6152
[tool.pdm.scripts]
62-
isort = {cmd = "isort cerbos/sdk tests"}
63-
black = {cmd = "black cerbos/sdk tests"}
64-
format = {composite = ["isort", "black"]}
65-
unasync = {cmd = "python utils/gen_unasync.py"}
66-
test = {cmd = "pytest"}
67-
pre_build = {composite = ["unasync", "format"]}
68-
pre_test = {composite = ["unasync", "format"]}
53+
isort = { cmd = "isort cerbos/sdk tests" }
54+
black = { cmd = "black cerbos/sdk tests" }
55+
format = { composite = ["isort", "black"] }
56+
unasync = { cmd = "python utils/gen_unasync.py" }
57+
test = { cmd = "pytest" }
58+
pre_build = { composite = ["unasync", "format"] }
59+
pre_test = { composite = ["unasync", "format"] }
6960

7061
[build-system]
7162
requires = ["pdm-backend"]
@@ -91,6 +82,8 @@ tag_release = "pdm run cz bump --changelog --increment"
9182
changelog = "pdm run cz changelog"
9283

9384
[tool.pyright]
85+
venvPath = "."
86+
venv = ".venv"
9487
extraPaths = ["__pypackages__/3.10/lib/"]
9588

9689
[tool.pytest.ini_options]
@@ -109,5 +102,5 @@ update_changelog_on_bump = true
109102

110103
[tool.commitizen.customize]
111104
commit_parser = "^(?P<change_type>feat|fix|enhancement|docs|chore)(\\\\(.*?\\\\))?:\\s(?P<message>.*)?"
112-
change_type_map = {"feat" = "Features", "fix" = "Bug fixes", "enhancement" = "Enhancements", "docs" = "Documentation", "chore" = "Chores"}
105+
change_type_map = { "feat" = "Features", "fix" = "Bug fixes", "enhancement" = "Enhancements", "docs" = "Documentation", "chore" = "Chores" }
113106
change_type_order = ["feat", "enhancement", "fix", "docs"]

0 commit comments

Comments
 (0)