Skip to content

Commit

Permalink
Enable extra Flake8 plugins (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
koenvervloesem committed Dec 25, 2022
1 parent bc2a120 commit dc7c370
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
12 changes: 11 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,20 @@ repos:
rev: 6.0.0
hooks:
- id: flake8
args: ['--ignore=E501']
additional_dependencies:
- flake8-bugbear
- pep8-naming
- flake8-length
- tryceratops
- flake8-return
- flake8-newspaper-style
- flake8-class-attributes-order
- flake8-warnings
- flake8-encodings
- flake8-simplify
- flake8-pie
- flake8-comprehensions
- flake8-picky-parentheses
# Documentation
- flake8-docstrings
- flake8-rst
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ extend_ignore = E203, W503
RST201,RST203,RST301,
# Missing type annotation for self in method
ANN101,
# Handled by flake8-length
E501, W505,
# Opinionated check by flake8-picky-parentheses
PAR101,
exclude =
.tox
build
Expand Down
7 changes: 4 additions & 3 deletions src/bluetooth_numbers/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ def __missing__(self, key: UUID | int) -> str:
if isinstance(key, UUID):
try:
uuid16_key = uuid128_to_uuid16(key)
return self[uuid16_key]
except NonStandardUUIDError as error:
raise UnknownUUIDError(key) from error
else:
return self[uuid16_key]
elif is_uint16(key):
raise UnknownUUIDError(key)
else:
raise No16BitIntegerError(key)

raise No16BitIntegerError(key)
41 changes: 20 additions & 21 deletions src/bluetooth_numbers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,6 @@ def normalize_oui(oui: str) -> str:
raise WrongOUIFormatError(oui)


def is_uint16(number: int) -> bool:
"""Check whether a number is a 16-bit unsigned integer.
Args:
number (int): The number to check.
Returns:
bool: ``True`` if `number` is a 16-bit unsigned integer, ``False`` otherwise.
Examples:
>>> from bluetooth_numbers.utils import is_uint16
>>> is_uint16(0x1800)
True
>>> is_uint16(-1)
False
"""
return isinstance(number, int) and 0 <= number <= 0xFFFF


def uuid128_to_uuid16(uuid128: UUID) -> int:
"""Convert a 128-bit standard Bluetooth UUID to a 16-bit UUID.
Expand All @@ -114,8 +95,7 @@ def uuid128_to_uuid16(uuid128: UUID) -> int:
raise NonStandardUUIDError(uuid128_masked)

# If it is, extract the 16-bit UUID
uuid16 = int.from_bytes(uuid128.bytes[2:4], "big")
return uuid16
return int.from_bytes(uuid128.bytes[2:4], "big")


def uuid16_to_uuid128(uuid16: int) -> UUID:
Expand Down Expand Up @@ -169,3 +149,22 @@ def uint16_to_hex(number: int) -> str:
raise No16BitIntegerError(number)

return f"{number:#0{6}x}"


def is_uint16(number: int) -> bool:
"""Check whether a number is a 16-bit unsigned integer.
Args:
number (int): The number to check.
Returns:
bool: ``True`` if `number` is a 16-bit unsigned integer, ``False`` otherwise.
Examples:
>>> from bluetooth_numbers.utils import is_uint16
>>> is_uint16(0x1800)
True
>>> is_uint16(-1)
False
"""
return isinstance(number, int) and 0 <= number <= 0xFFFF
2 changes: 1 addition & 1 deletion tests/test_companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(0x0499, "Ruuvi Innovations Ltd."),
(
0xFFFF,
"Bluetooth SIG Specification Reserved Default Vendor ID for Remote Devices Without Device ID Service Record.", # noqa: E501
"Bluetooth SIG Specification Reserved Default Vendor ID for Remote Devices Without Device ID Service Record.",
),
],
)
Expand Down

0 comments on commit dc7c370

Please sign in to comment.