Skip to content
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

chore(python-sdk): remove Optional typings #451

Merged
merged 1 commit into from
Nov 7, 2024
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
16 changes: 1 addition & 15 deletions config/clients/python/template/src/configuration.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import sys
import http
import urllib
import urllib3
from typing import Optional

from {{packageName}}.exceptions import FgaValidationException, ApiValueError
from {{packageName}}.telemetry.attributes import TelemetryAttribute
Expand Down Expand Up @@ -164,20 +163,7 @@ class Configuration:
server_operation_index=None, server_operation_variables=None,
ssl_ca_cert=None,
api_url=None, # TODO: restructure when removing api_scheme/api_host
telemetry: Optional[
dict[
TelemetryConfigurationType | str,
TelemetryMetricsConfiguration
| dict[
TelemetryHistogram | TelemetryCounter,
TelemetryMetricConfiguration
| dict[TelemetryAttribute, bool]
| None,
]
| None,
]
] = None,
):
telemetry: dict[TelemetryConfigurationType | str, TelemetryMetricsConfiguration | dict[TelemetryHistogram | TelemetryCounter, TelemetryMetricConfiguration | dict[TelemetryAttribute, bool] | None] | None] | None = None):
"""Constructor
"""
self._url = api_url
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
import urllib
from typing import NamedTuple, Optional
from typing import NamedTuple

from aiohttp import ClientResponse
from urllib3 import HTTPResponse
Expand Down Expand Up @@ -88,7 +88,7 @@ class TelemetryAttributes:

@staticmethod
def get(
name: Optional[str] = None,
name: str | None = None,
) -> list[TelemetryAttribute] | TelemetryAttribute | None:
if name is None:
return TelemetryAttributes._attributes
Expand Down Expand Up @@ -233,11 +233,9 @@ class TelemetryAttributes:

@staticmethod
def fromResponse(
response: Optional[
HTTPResponse | RESTResponse | ClientResponse | ApiException
] = None,
credentials: Optional[Credentials] = None,
attributes: Optional[dict[TelemetryAttribute, str | int]] = None,
response: HTTPResponse | RESTResponse | ClientResponse | ApiException | None = None,
credentials: Credentials | None = None,
attributes: dict[TelemetryAttribute, str | int] | None = None,
) -> dict[TelemetryAttribute, str | int]:
response_model_id = None
response_query_duration = None
Expand Down Expand Up @@ -294,8 +292,8 @@ class TelemetryAttributes:
@staticmethod
def coalesceAttributeValue(
attribute: TelemetryAttribute,
value: Optional[int | float] = None,
attributes: Optional[dict[TelemetryAttribute, str | int]] = None,
value: int | float | None = None,
attributes: dict[TelemetryAttribute, str | int] | None = None,
) -> int | float | None:
if value is None:
if attribute in attributes:
Expand All @@ -317,4 +315,4 @@ class TelemetryAttributes:
if attribute.format == "string":
value = str(value)

return value
return value
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import NamedTuple, Optional
from typing import NamedTuple

from {{packageName}}.telemetry.attributes import TelemetryAttribute, TelemetryAttributes
from {{packageName}}.telemetry.counters import TelemetryCounter, TelemetryCounters
Expand All @@ -15,22 +15,22 @@ class TelemetryMetricConfiguration:

def __init__(
self,
config: Optional[dict[TelemetryAttribute, bool]] = None,
fga_client_request_client_id: Optional[bool] = None,
fga_client_request_method: Optional[bool] = None,
fga_client_request_model_id: Optional[bool] = None,
fga_client_request_store_id: Optional[bool] = None,
fga_client_response_model_id: Optional[bool] = None,
fga_client_user: Optional[bool] = None,
http_client_request_duration: Optional[bool] = None,
http_host: Optional[bool] = None,
http_request_method: Optional[bool] = None,
http_request_resend_count: Optional[bool] = None,
http_response_status_code: Optional[bool] = None,
http_server_request_duration: Optional[bool] = None,
url_scheme: Optional[bool] = None,
url_full: Optional[bool] = None,
user_agent_original: Optional[bool] = None,
config: dict[TelemetryAttribute, bool] | None = None,
fga_client_request_client_id: bool | None = None,
fga_client_request_method: bool | None = None,
fga_client_request_model_id: bool | None = None,
fga_client_request_store_id: bool | None = None,
fga_client_response_model_id: bool | None = None,
fga_client_user: bool | None = None,
http_client_request_duration: bool | None = None,
http_host: bool | None = None,
http_request_method: bool | None = None,
http_request_resend_count: bool | None = None,
http_response_status_code: bool | None = None,
http_server_request_duration: bool | None = None,
url_scheme: bool | None = None,
url_full: bool | None = None,
user_agent_original: bool | None = None,
):
"""
Initialize a new instance of the `TelemetryMetricConfiguration` class.
Expand Down Expand Up @@ -467,8 +467,8 @@ class TelemetryMetricConfiguration:

def configure(
self,
config: Optional[dict[TelemetryAttribute | str, bool]] = None,
clear: Optional[bool] = False,
config: dict[TelemetryAttribute | str, bool] | None = None,
clear: bool | None = False,
) -> None:
"""
Configure the telemetry metric.
Expand Down Expand Up @@ -508,7 +508,7 @@ class TelemetryMetricConfiguration:
self._valid = None

def getAttributes(
self, filter_enabled: Optional[bool] = True
self, filter_enabled: bool | None = True
) -> dict[TelemetryAttribute, bool]:
"""
Returns a list of supported attributes. If `filter_enabled` is `True`, only enabled attributes are returned.
Expand All @@ -529,7 +529,7 @@ class TelemetryMetricConfiguration:

return attributes

def isEnabled(self, attribute: Optional[TelemetryAttribute] = None) -> bool:
def isEnabled(self, attribute: TelemetryAttribute | None = None) -> bool:
"""
Check if this metric is enabled for telemetry, based on whether any attributes are enabled.

Expand All @@ -546,7 +546,7 @@ class TelemetryMetricConfiguration:

return False

def isValid(self, raise_exception: Optional[bool] = False) -> bool:
def isValid(self, raise_exception: bool = False) -> bool:
"""
Validate the telemetry metric configuration.

Expand Down Expand Up @@ -603,16 +603,11 @@ class TelemetryMetricsConfiguration:

def __init__(
self,
config: Optional[
dict[
TelemetryHistogram | TelemetryCounter,
TelemetryMetricConfiguration | None,
]
] = None,
fga_client_credentials_request: Optional[TelemetryMetricConfiguration] = None,
fga_client_request_duration: Optional[TelemetryMetricConfiguration] = None,
fga_client_query_duration: Optional[TelemetryMetricConfiguration] = None,
fga_client_request: Optional[TelemetryMetricConfiguration] = None,
config: dict[TelemetryHistogram | TelemetryCounter, TelemetryMetricConfiguration | None] | None = None,
fga_client_credentials_request: TelemetryMetricConfiguration | None = None,
fga_client_request_duration: TelemetryMetricConfiguration | None = None,
fga_client_query_duration: TelemetryMetricConfiguration | None = None,
fga_client_request: TelemetryMetricConfiguration | None = None,
):
"""
Initialize a new instance of the `TelemetryMetricsConfiguration` class.
Expand Down Expand Up @@ -748,13 +743,8 @@ class TelemetryMetricsConfiguration:

def configure(
self,
config: Optional[
dict[
TelemetryHistogram | TelemetryCounter | str,
TelemetryMetricConfiguration | dict[TelemetryAttribute, bool] | None,
]
] = None,
clear: Optional[bool] = False,
config: dict[TelemetryHistogram | TelemetryCounter | str, TelemetryMetricConfiguration | dict[TelemetryAttribute, bool] | None] | None = None,
clear: bool = False,
) -> None:
"""
Configure metrics reporting for telemetry.
Expand Down Expand Up @@ -800,7 +790,7 @@ class TelemetryMetricsConfiguration:
self._valid = None

def getMetrics(
self, filter_enabled: Optional[bool] = True
self, filter_enabled: bool = True
) -> dict[
TelemetryHistogram | TelemetryCounter, TelemetryMetricConfiguration | None
]:
Expand All @@ -824,7 +814,7 @@ class TelemetryMetricsConfiguration:
return metrics

def isEnabled(
self, metric: Optional[TelemetryCounter | TelemetryHistogram] = None
self, metric: TelemetryCounter | TelemetryHistogram | None = None
) -> bool:
"""
Check if a metric is enabled for telemetry.
Expand All @@ -846,7 +836,7 @@ class TelemetryMetricsConfiguration:

return False

def isValid(self, raise_exception: Optional[bool] = False) -> bool:
def isValid(self, raise_exception: bool = False) -> bool:
"""
Validate the telemetry metrics configuration.

Expand Down Expand Up @@ -902,7 +892,7 @@ class TelemetryConfigurations:

@staticmethod
def get(
name: Optional[str] = None,
name: str | None = None,
) -> list[TelemetryConfigurationType] | TelemetryConfigurationType | None:
if name is None:
return TelemetryConfigurations._configurations
Expand All @@ -920,8 +910,8 @@ class TelemetryConfiguration:

def __init__(
self,
config: Optional[dict[str, TelemetryMetricsConfiguration | None]] = None,
metrics: Optional[TelemetryMetricsConfiguration] = None,
config: dict[str, TelemetryMetricsConfiguration | None] | None = None,
metrics: TelemetryMetricsConfiguration | None = None,
):
"""
Initialize a new instance of the `TelemetryConfiguration` class.
Expand Down Expand Up @@ -973,20 +963,8 @@ class TelemetryConfiguration:

def configure(
self,
config: Optional[
dict[
TelemetryConfigurationType | str,
TelemetryMetricsConfiguration
| dict[
TelemetryHistogram | TelemetryCounter,
TelemetryMetricConfiguration
| dict[TelemetryAttribute, bool]
| None,
]
| None,
]
] = None,
clear: Optional[bool] = False,
config: dict[TelemetryConfigurationType | str, TelemetryMetricsConfiguration | dict[TelemetryHistogram | TelemetryCounter, TelemetryMetricConfiguration | dict[TelemetryAttribute, bool] | None] | None] | None = None,
clear: bool = False,
) -> None:
"""
Configure telemetry reporting.
Expand Down Expand Up @@ -1028,7 +1006,7 @@ class TelemetryConfiguration:
self._valid = None

def getConfigurations(
self, filter_enabled: Optional[bool] = True
self, filter_enabled: bool = True
) -> dict[TelemetryConfigurationType, TelemetryMetricsConfiguration | None]:
"""
Returns a list of supported reporting contexts. If `filter_enabled` is `True`, only enabled contexts are returned.
Expand All @@ -1050,7 +1028,7 @@ class TelemetryConfiguration:
return contexts

def isEnabled(
self, configuration: Optional[TelemetryConfigurationType] = None
self, configuration: TelemetryConfigurationType | None = None
) -> bool:
"""
Check if telemetry is enabled.
Expand All @@ -1070,7 +1048,7 @@ class TelemetryConfiguration:

return False

def isValid(self, raise_exception: Optional[bool] = False) -> bool:
def isValid(self, raise_exception: bool = False) -> bool:
"""
Validate the telemetry configuration.

Expand Down Expand Up @@ -1122,4 +1100,4 @@ def isMetricEnabled(
if isinstance(config, TelemetryMetricsConfiguration):
return config.isEnabled(metric)

return False
return False
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import NamedTuple, Optional
from typing import NamedTuple


class TelemetryCounter(NamedTuple):
Expand All @@ -25,7 +25,7 @@ class TelemetryCounters:

@staticmethod
def get(
name: Optional[str] = None,
name: str | None = None,
) -> list[TelemetryCounter] | TelemetryCounter | None:
if name is None:
return TelemetryCounters._counters
Expand All @@ -34,4 +34,4 @@ class TelemetryCounters:
if counter.name == name:
return counter

return None
return None
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import NamedTuple, Optional
from typing import NamedTuple


class TelemetryHistogram(NamedTuple):
Expand All @@ -24,7 +24,7 @@ class TelemetryHistograms:

@staticmethod
def get(
name: Optional[str] = None,
name: str | None = None,
) -> list[TelemetryHistogram] | TelemetryHistogram | None:
if name is None:
return TelemetryHistograms._histograms
Expand All @@ -33,4 +33,4 @@ class TelemetryHistograms:
if histogram.name == name:
return histogram

return None
return None
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from opentelemetry.metrics import Counter, Histogram, Meter, get_meter

from {{packageName}}.telemetry.attributes import (
Expand All @@ -21,9 +19,9 @@ class TelemetryMetrics:

def __init__(
self,
meter: Optional[Meter] = None,
counters: Optional[dict[str, Counter]] = None,
histograms: Optional[dict[str, Histogram]] = None,
meter: Meter | None = None,
counters: dict[str, Counter] | None = None,
histograms: dict[str, Histogram] | None = None,
):
self._meter = meter
self._counters = counters or {}
Expand Down Expand Up @@ -171,4 +169,4 @@ class TelemetryMetrics:
if value is not None:
histogram.record(amount=value, attributes=attributes)

return histogram
return histogram
Loading