Skip to content

Commit

Permalink
typing: Annotate openstack.format
Browse files Browse the repository at this point in the history
Change-Id: I38184277951e0f8decd16d98d9f3f24a7d878b63
Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Feb 19, 2025
1 parent 1d82105 commit 6c764f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 8 additions & 4 deletions openstack/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
# License for the specific language governing permissions and limitations
# under the License.

import typing as ty

class Formatter:
_T = ty.TypeVar('_T')


class Formatter(ty.Generic[_T]):
@classmethod
def deserialize(cls, value):
def deserialize(cls, value: ty.Any) -> _T:
"""Return a formatted object representing the value"""
raise NotImplementedError


class BoolStr(Formatter):
class BoolStr(Formatter[bool]):
@classmethod
def deserialize(cls, value):
def deserialize(cls, value: ty.Any) -> bool:
"""Convert a boolean string to a boolean"""
expr = str(value).lower()
if "true" == expr:
Expand Down
4 changes: 2 additions & 2 deletions openstack/key_manager/v1/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from openstack import format


class HREFToUUID(format.Formatter):
class HREFToUUID(format.Formatter[str]):
@classmethod
def deserialize(cls, value):
def deserialize(cls, value: str) -> str:
"""Convert a HREF to the UUID portion"""
parts = parse.urlsplit(value)

Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ exclude = '''
)
'''

[[tool.mypy.overrides]]
module = ["openstack.format"]
warn_return_any = true
disallow_untyped_decorators = true
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
no_implicit_reexport = true

[[tool.mypy.overrides]]
module = ["openstack.tests.unit.*"]
ignore_errors = true
Expand Down

0 comments on commit 6c764f1

Please sign in to comment.