Skip to content

Commit

Permalink
Merge pull request #2339 from VWS-Python/immutable-types-len
Browse files Browse the repository at this point in the history
Progress towards using more immutable data structures
  • Loading branch information
adamtheturtle committed Sep 28, 2024
2 parents 0296edc + cb8567a commit c3125ed
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/mock_vws/_mock_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import json
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any

Expand All @@ -23,7 +24,7 @@ class Route:

route_name: str
path_pattern: str
http_methods: frozenset[str]
http_methods: Iterable[str]


@beartype
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_query_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import base64
import io
import uuid
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from email.message import EmailMessage
from typing import Any

Expand All @@ -27,7 +27,7 @@ def get_query_match_response_text(
request_body: bytes,
request_method: str,
request_path: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
query_match_checker: ImageMatcher,
) -> str:
"""
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_query_validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Input validators to use in the mock query API.
"""

from collections.abc import Mapping
from collections.abc import Iterable, Mapping

from beartype import beartype

Expand Down Expand Up @@ -47,7 +47,7 @@ def run_query_validators(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Run all validators.
Expand Down
6 changes: 3 additions & 3 deletions src/mock_vws/_query_validators/auth_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import logging
from collections.abc import Mapping
from collections.abc import Iterable, Mapping

from beartype import beartype

Expand Down Expand Up @@ -65,7 +65,7 @@ def validate_auth_header_number_of_parts(
def validate_client_key_exists(
*,
request_headers: Mapping[str, str],
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate the authorization header includes a client key for a database.
Expand Down Expand Up @@ -116,7 +116,7 @@ def validate_authorization(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate the authorization header given to the query endpoint.
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_query_validators/project_state_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import logging
from collections.abc import Mapping
from collections.abc import Iterable, Mapping

from beartype import beartype

Expand All @@ -21,7 +21,7 @@ def validate_project_state(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate the state of the project.
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_services_validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Input validators to use in the mock.
"""

from collections.abc import Mapping
from collections.abc import Iterable, Mapping

from mock_vws.database import VuforiaDatabase

Expand Down Expand Up @@ -56,7 +56,7 @@ def run_services_validators(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Run all validators.
Expand Down
6 changes: 3 additions & 3 deletions src/mock_vws/_services_validators/auth_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import logging
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from http import HTTPStatus

from beartype import beartype
Expand Down Expand Up @@ -38,7 +38,7 @@ def validate_auth_header_exists(*, request_headers: Mapping[str, str]) -> None:
def validate_access_key_exists(
*,
request_headers: Mapping[str, str],
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate the authorization header includes an access key for a database.
Expand Down Expand Up @@ -95,7 +95,7 @@ def validate_authorization(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate the authorization header given to a VWS endpoint.
Expand Down
6 changes: 3 additions & 3 deletions src/mock_vws/_services_validators/name_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import json
import logging
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from http import HTTPMethod, HTTPStatus

from beartype import beartype
Expand Down Expand Up @@ -121,7 +121,7 @@ def validate_name_length(*, request_body: bytes) -> None:
@beartype
def validate_name_does_not_exist_new_target(
*,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
request_body: bytes,
request_headers: Mapping[str, str],
request_method: str,
Expand Down Expand Up @@ -182,7 +182,7 @@ def validate_name_does_not_exist_existing_target(
request_body: bytes,
request_method: str,
request_path: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate that the name does not exist for any existing target apart from
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_services_validators/project_state_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import logging
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from http import HTTPMethod

from beartype import beartype
Expand All @@ -23,7 +23,7 @@ def validate_project_state(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate the state of the project.
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_services_validators/target_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import logging
from collections.abc import Mapping
from collections.abc import Iterable, Mapping

from beartype import beartype

Expand All @@ -21,7 +21,7 @@ def validate_target_id_exists(
request_headers: Mapping[str, str],
request_body: bytes,
request_method: str,
databases: set[VuforiaDatabase],
databases: Iterable[VuforiaDatabase],
) -> None:
"""
Validate that if a target ID is given, it exists in the database matching
Expand Down
13 changes: 9 additions & 4 deletions src/mock_vws/target_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
A fake implementation of a Vuforia target manager.
"""

from typing import TYPE_CHECKING

from beartype import beartype

from mock_vws.database import VuforiaDatabase

if TYPE_CHECKING:
from collections.abc import Iterable


@beartype
class TargetManager:
Expand All @@ -17,7 +22,7 @@ def __init__(self) -> None:
"""
Create a target manager with no databases.
"""
self._databases: set[VuforiaDatabase] = set()
self._databases: Iterable[VuforiaDatabase] = set()

def remove_database(self, database: VuforiaDatabase) -> None:
"""
Expand All @@ -29,7 +34,7 @@ def remove_database(self, database: VuforiaDatabase) -> None:
Raises:
KeyError: The database is not in the target manager.
"""
self._databases.remove(database)
self._databases = {db for db in self._databases if db != database}

def add_database(self, database: VuforiaDatabase) -> None:
"""
Expand Down Expand Up @@ -78,11 +83,11 @@ def add_database(self, database: VuforiaDatabase) -> None:
message = message_fmt.format(key_name=key_name, value=new)
raise ValueError(message)

self._databases.add(database)
self._databases = {*self._databases, database}

@property
def databases(self) -> set[VuforiaDatabase]:
"""
All cloud databases.
"""
return self._databases
return set(self._databases)

0 comments on commit c3125ed

Please sign in to comment.