Skip to content

Commit

Permalink
fix: Use pre-3.9 dict merge operator (#50)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Sambigeara authored Mar 7, 2024
1 parent 029cef0 commit 5b65166
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Bug fixes

- Use pre-3.9 dict merge operator

## v0.10.4 (2024-01-24)

### Bug fixes
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ You can pass additional options in the `channel_options` dict.
Available options are described [here](https://github.com/grpc/grpc/blob/7536d8a849c0096e4c968e7730306872bb5ec674/include/grpc/impl/grpc_types.h).
The argument is of type `dict[str, Any]` where the `Any` value must match the expected type defined in the previous link.

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).

NOTE: We provide this as a generic method to set arbitrary options for particular use cases.
For purely demonstrative purposes, our example below overrides `grpc.ssl_target_name_override`, which is certainly not recommended practice for production applications.

Expand Down
14 changes: 7 additions & 7 deletions cerbos/sdk/_async/_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ssl
import uuid
from functools import wraps
from typing import Any, Dict, List, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union

import grpc

Expand Down Expand Up @@ -79,8 +79,8 @@ class AsyncClientBase:
def __init__(
self,
host: str,
creds: grpc.ChannelCredentials,
methods: List[Dict[str, str]] = None,
creds: Optional[grpc.ChannelCredentials],
methods: Optional[List[Dict[str, str]]] = None,
tls_verify: TLSVerify = False,
timeout_secs: Union[float, None] = None,
request_retries: int = 0,
Expand Down Expand Up @@ -123,10 +123,10 @@ def __init__(
}

if channel_options:
options |= channel_options
options = {**options, **channel_options}

opts = [(k, v) for k, v in options.items()]
if tls_verify:
if tls_verify and creds:
self._channel = grpc.aio.secure_channel(
host,
credentials=creds,
Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(
wait_for_ready: bool = False,
channel_options: Union[Dict[str, Any], None] = None,
):
creds: grpc.ChannelCredentials = None
creds: Optional[grpc.ChannelCredentials] = None
if tls_verify:
cert = get_cert(tls_verify)
creds = grpc.ssl_channel_credentials(cert)
Expand Down Expand Up @@ -448,7 +448,7 @@ def __init__(
admin_credentials = admin_credentials or AdminCredentials()
self._creds_metadata = admin_credentials.metadata()

creds: grpc.ChannelCredentials = None
creds: Optional[grpc.ChannelCredentials] = None
if tls_verify:
cert = get_cert(tls_verify)
creds = grpc.ssl_channel_credentials(cert)
Expand Down
14 changes: 7 additions & 7 deletions cerbos/sdk/_sync/_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ssl
import uuid
from functools import wraps
from typing import Any, Dict, List, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union

import grpc

Expand Down Expand Up @@ -79,8 +79,8 @@ class SyncClientBase:
def __init__(
self,
host: str,
creds: grpc.ChannelCredentials,
methods: List[Dict[str, str]] = None,
creds: Optional[grpc.ChannelCredentials],
methods: Optional[List[Dict[str, str]]] = None,
tls_verify: TLSVerify = False,
timeout_secs: Union[float, None] = None,
request_retries: int = 0,
Expand Down Expand Up @@ -123,10 +123,10 @@ def __init__(
}

if channel_options:
options |= channel_options
options = {**options, **channel_options}

opts = [(k, v) for k, v in options.items()]
if tls_verify:
if tls_verify and creds:
self._channel = grpc.secure_channel(
host,
credentials=creds,
Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(
wait_for_ready: bool = False,
channel_options: Union[Dict[str, Any], None] = None,
):
creds: grpc.ChannelCredentials = None
creds: Optional[grpc.ChannelCredentials] = None
if tls_verify:
cert = get_cert(tls_verify)
creds = grpc.ssl_channel_credentials(cert)
Expand Down Expand Up @@ -448,7 +448,7 @@ def __init__(
admin_credentials = admin_credentials or AdminCredentials()
self._creds_metadata = admin_credentials.metadata()

creds: grpc.ChannelCredentials = None
creds: Optional[grpc.ChannelCredentials] = None
if tls_verify:
cert = get_cert(tls_verify)
creds = grpc.ssl_channel_credentials(cert)
Expand Down
37 changes: 15 additions & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
name = "cerbos"
description = "SDK for working with Cerbos: an open core, language-agnostic, scalable authorization solution"
readme = "README.md"
authors = [
{name = "Cerbos Developers", email = "sdk+python@cerbos.dev"},
]
license = {text = "Apache-2.0"}
authors = [{ name = "Cerbos Developers", email = "sdk+python@cerbos.dev" }]
license = { text = "Apache-2.0" }
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
Expand All @@ -27,25 +25,18 @@ requires-python = ">=3.8"
dynamic = ["version"]



[project.urls]
Homepage = "https://cerbos.dev"

[project.optional-dependencies]
testcontainers = [
"testcontainers>=3.5.3",
]
testcontainers = ["testcontainers>=3.5.3"]

[tool.pdm.version]
source = "scm"

[tool.pdm.dev-dependencies]
lint = [
"black>=22.3.0",
"isort>=5.10.1"]
test = [
"pytest>=7.3.1",
]
lint = ["black>=22.3.0", "isort>=5.10.1"]
test = ["pytest>=7.3.1"]
tools = [
"unasync>=0.5.0",
"setuptools>=63.2.0",
Expand All @@ -59,13 +50,13 @@ tools = [
includes = ["cerbos/", "google/", "buf/"]

[tool.pdm.scripts]
isort = {cmd = "isort cerbos/sdk tests"}
black = {cmd = "black cerbos/sdk tests"}
format = {composite = ["isort", "black"]}
unasync = {cmd = "python utils/gen_unasync.py"}
test = {cmd = "pytest"}
pre_build = {composite = ["unasync", "format"]}
pre_test = {composite = ["unasync", "format"]}
isort = { cmd = "isort cerbos/sdk tests" }
black = { cmd = "black cerbos/sdk tests" }
format = { composite = ["isort", "black"] }
unasync = { cmd = "python utils/gen_unasync.py" }
test = { cmd = "pytest" }
pre_build = { composite = ["unasync", "format"] }
pre_test = { composite = ["unasync", "format"] }

[build-system]
requires = ["pdm-backend"]
Expand All @@ -91,6 +82,8 @@ tag_release = "pdm run cz bump --changelog --increment"
changelog = "pdm run cz changelog"

[tool.pyright]
venvPath = "."
venv = ".venv"
extraPaths = ["__pypackages__/3.10/lib/"]

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

[tool.commitizen.customize]
commit_parser = "^(?P<change_type>feat|fix|enhancement|docs|chore)(\\\\(.*?\\\\))?:\\s(?P<message>.*)?"
change_type_map = {"feat" = "Features", "fix" = "Bug fixes", "enhancement" = "Enhancements", "docs" = "Documentation", "chore" = "Chores"}
change_type_map = { "feat" = "Features", "fix" = "Bug fixes", "enhancement" = "Enhancements", "docs" = "Documentation", "chore" = "Chores" }
change_type_order = ["feat", "enhancement", "fix", "docs"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5b65166

Please sign in to comment.