Skip to content

Commit

Permalink
✨ Add type checking support (#285)
Browse files Browse the repository at this point in the history
* ✨ Add type checking support and improve type hints across the codebase

* 🔧 Remove type ignores for improved type checking in Pendulum and Semantic Version classes
  • Loading branch information
yezz123 authored Jan 15, 2025
1 parent 01866e6 commit 5ded2a1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v5.0.0
hooks:
- id: no-commit-to-branch # prevent direct commits to the `main` branch
- id: check-yaml
Expand All @@ -24,3 +24,10 @@ repos:
types: [python]
language: system
pass_filenames: false
- id: Typecheck
name: Typecheck
entry: make
args: [typecheck]
types: [python]
language: system
pass_filenames: false
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ lint:
uv run ruff format --check
uv run ruff check

.PHONY: typecheck # Typecheck the code
typecheck:
uv run mypy pydantic_extra_types

.PHONY: test
test:
uv run pytest
Expand Down
3 changes: 2 additions & 1 deletion pydantic_extra_types/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa
def __get_pydantic_json_schema__(
cls, schema: core_schema.CoreSchema, handler: GetCoreSchemaHandler
) -> dict[str, Any]:
return handler(schema)
# Cast the return value to dict[str, Any]
return dict(handler(schema))
2 changes: 1 addition & 1 deletion pydantic_extra_types/phone_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PhoneNumberValidator:
supported_regions (list[str]): The supported regions. If empty, all regions are supported (default).
Returns:
str: The formatted phone number.
The formatted phone number.
Example:
MyNumberType = Annotated[
Expand Down
4 changes: 4 additions & 0 deletions pydantic_extra_types/semantic_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ def __get_pydantic_json_schema__(
pattern=r'^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
)
)

@classmethod
def validate_from_str(cls, value: str) -> 'SemanticVersion':
return cls.parse(value)

0 comments on commit 5ded2a1

Please sign in to comment.