Skip to content

Commit

Permalink
Add modern typing (#10)
Browse files Browse the repository at this point in the history
This adds typing syntax as modern as possible on Python 3.7
  • Loading branch information
koenvervloesem committed Dec 25, 2022
1 parent 29567b8 commit 3cadeb1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ repos:
args: ['--ignore=E501']
additional_dependencies:
- flake8-bugbear
- pep8-naming
# Documentation
- flake8-docstrings
- flake8-rst
- flake8-rst-docstrings
- pep8-naming
# Typing
- flake8-future-annotations
- flake8-annotations
- flake-type-annotations-plugin

- repo: https://github.com/pycqa/pylint
rev: v2.15.9
Expand Down
17 changes: 9 additions & 8 deletions scripts/generate_modules.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Generate Python modules for Bluetooth numbers."""
from __future__ import annotations

import json
import re
from pathlib import Path
from typing import Dict, Tuple

from jinja2 import Environment, FileSystemLoader

Expand All @@ -19,7 +20,7 @@
env = Environment(loader=file_loader)


def generate_uuid16_dictionary(kind: str) -> Dict[int, str]:
def generate_uuid16_dictionary(kind: str) -> dict[int, str]:
"""Generate 16-bit UUID dictionary for a module.
The parameter `kind` should be "sdo_service".
Expand All @@ -38,7 +39,7 @@ def generate_uuid16_dictionary(kind: str) -> Dict[int, str]:
return uuid16_dict


def generate_uuid_dictionaries(kind: str) -> Tuple[Dict[int, str], Dict[str, str]]:
def generate_uuid_dictionaries(kind: str) -> tuple[dict[int, str], dict[str, str]]:
"""Generate UUID dictionaries for a module.
The parameter `kind` should be "service", "characteristic", or "descriptor".
Expand All @@ -62,7 +63,7 @@ def generate_uuid_dictionaries(kind: str) -> Tuple[Dict[int, str], Dict[str, str


def generate_uuid_module(
kind: str, uuid16_dict: Dict[int, str], uuid128_dict: Dict[str, str]
kind: str, uuid16_dict: dict[int, str], uuid128_dict: dict[str, str]
) -> None:
"""Generate Python module for UUIDs.
Expand All @@ -76,7 +77,7 @@ def generate_uuid_module(
)


def generate_cic_dictionary() -> Dict[str, str]:
def generate_cic_dictionary() -> dict[str, str]:
"""Generate Company ID Code dictionary for a module.
Returns a dict with CIC keys and their name.
Expand All @@ -93,15 +94,15 @@ def generate_cic_dictionary() -> Dict[str, str]:
return cic_dict


def generate_cic_module(cic_dict: Dict[str, str]) -> None:
def generate_cic_module(cic_dict: dict[str, str]) -> None:
"""Generate Python module for Company ID Codes."""
template = env.get_template(CIC_TEMPLATE)
with (Path(CODE_DIR) / "_companies.py").open("w") as python_file:
python_file.write("# pylint: skip-file\n")
python_file.write(template.render(cics=cic_dict))


def generate_oui_dictionary() -> Dict[str, str]:
def generate_oui_dictionary() -> dict[str, str]:
"""Generate OUI dictionary for a module.
Returns a dict with OUI prefixes and their name.
Expand All @@ -117,7 +118,7 @@ def generate_oui_dictionary() -> Dict[str, str]:
return oui_dict


def generate_oui_module(oui_dict: Dict[str, str]) -> None:
def generate_oui_module(oui_dict: dict[str, str]) -> None:
"""Generate Python module for OUIs."""
template = env.get_template(OUI_TEMPLATE)
with (Path(CODE_DIR) / "_ouis.py").open("w") as python_file:
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ extend_ignore = E203, W503
# Google Python style is not RST until after processed by Napoleon
# See https://github.com/peterjc/flake8-rst-docstrings/issues/17
RST201,RST203,RST301,
# Missing type annotation for self in method
ANN101,
exclude =
.tox
build
Expand Down
10 changes: 6 additions & 4 deletions src/bluetooth_numbers/dicts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Module with specialized dictionary classes for UUIDs, CICs and OUIs."""
from __future__ import annotations

from typing import Dict, Union
from uuid import UUID

Expand All @@ -17,7 +19,7 @@
)


class CICDict(Dict[int, str]):
class CICDict(Dict[int, str]): # noqa
"""Dictionary class to hold 16-bit company codes and their names.
You can use this class as a dict with the following differences:
Expand Down Expand Up @@ -47,7 +49,7 @@ def __missing__(self, key: int) -> str:
raise No16BitIntegerError(key)


class OUIDict(Dict[str, str]):
class OUIDict(Dict[str, str]): # noqa
"""Dictionary class to hold OUIs and their names.
You can use this class as a dict with the following differences:
Expand Down Expand Up @@ -83,7 +85,7 @@ def __missing__(self, key: str) -> str:
return self[normalize_oui(key)]


class UUIDDict(Dict[Union[UUID, int], str]):
class UUIDDict(Dict[Union[UUID, int], str]): # noqa
"""Dictionary class to hold 16-bit and 128-bit standard UUID keys and descriptions.
You can use this class as a dict for Bluetooth UUIDs, with the following
Expand Down Expand Up @@ -111,7 +113,7 @@ class UUIDDict(Dict[Union[UUID, int], str]):
bluetooth_numbers.exceptions.No16BitIntegerError: 6.5
"""

def __missing__(self, key: Union[UUID, int]) -> str:
def __missing__(self, key: UUID | int) -> str:
"""Try the key converted to 16-bit UUID."""
if isinstance(key, UUID):
try:
Expand Down

0 comments on commit 3cadeb1

Please sign in to comment.