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): check formatting #44

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .github/workflows/python_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ jobs:
pip install '.[development]'
pip install -e .

- name: Format check
working-directory: python
run: |
black lib/sift_py lib/sift_internal/ --check

- name: Run pytest
working-directory: python
run: |
Expand Down
8 changes: 7 additions & 1 deletion python/lib/sift_internal/convert/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

ProtobufMessage = Message


class AsProtobuf(ABC):
"""
Abstract base class used to create create sub-types that can be treated
as an object that can be converted into an instance of `ProtobufMessage`.
"""

@abstractmethod
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
"""
Expand All @@ -19,11 +21,15 @@ def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:


T = TypeVar("T", bound=ProtobufMessage)


def try_cast_pb(value: AsProtobuf, target_klass: Type[T]) -> T:
"""
Tries to cast the `value` to `target_klass`, otherwise, returns a `TypeError`.
"""
value_pb = value.as_pb(target_klass)
if isinstance(value_pb, target_klass):
return cast(target_klass, value_pb)
raise TypeError(f"Expected a '{target_klass.__module__}{target_klass.__name__}' but got {value.__module__}{value.__class__.__name__}")
raise TypeError(
f"Expected a '{target_klass.__module__}{target_klass.__name__}' but got {value.__module__}{value.__class__.__name__}"
)
1 change: 1 addition & 0 deletions python/lib/sift_internal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

T = TypeVar("T")


def any_as(value: Any, target_klass: Type[T]) -> Optional[T]:
"""
Attempts to convert `value` of type `Any` to `target_klass`, otherwise return `None`.
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
where = ["gen", "lib"]
where = ["lib"]

# The formatter
[tool.black]
Expand Down
2 changes: 1 addition & 1 deletion python/scripts/dev
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bootstrap() {

fmt() {
source venv/bin/activate
black lib/sift_py
black lib/sift_py lib/sift_internal
}

run_tests() {
Expand Down
Loading