Skip to content

Commit

Permalink
feat: Add has_annotation field to ParameterView. (#41)
Browse files Browse the repository at this point in the history
* feat: Add has_annotation field to ParameterView.

* fix: CVE in CI.
  • Loading branch information
DanCardin authored Oct 2, 2024
1 parent aa5d9b2 commit 483021b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ jobs:
run: pdm run make docs

- name: Save PR number
env:
PR_NUMBER: ${{ github.event.number }}
run: echo $PR_NUMBER > .pr_number
run: |
echo "${{ github.event.number }}" > .pr_number
- name: Upload artifact
uses: actions/upload-artifact@v3
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/docs-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ jobs:
path: docs-preview
name: docs-preview

- name: Set PR number
run: echo "PR_NUMBER=$(cat docs-preview/.pr_number)" >> $GITHUB_ENV
- name: Validate and set PR number
run: |
PR_NUMBER=$(cat docs-preview/.pr_number)
if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
echo "Invalid PR number: $PR_NUMBER"
exit 1
fi
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
- name: Deploy docs preview
uses: JamesIves/github-pages-deploy-action@v4
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ classifiers = [
"Intended Audience :: Developers",
]
name = "type-lens"
version = "0.2.2"
version = "0.2.3"
description = "type-lens is a Python template project designed to simplify the setup of a new project."
readme = "README.md"
license = { text = "MIT" }
Expand Down
2 changes: 1 addition & 1 deletion tests/test_callable_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def fn(foo): # type: ignore[no-untyped-def]
return foo

function_view = CallableView.from_callable(fn)
assert function_view.parameters == (ParameterView("foo", TypeView(Any)),)
assert function_view.parameters == (ParameterView("foo", TypeView(Any), has_annotation=False),)


def test_typed_param() -> None:
Expand Down
22 changes: 20 additions & 2 deletions type_lens/parameter_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,35 @@ class ParameterView:
"name": "The name of the parameter.",
"default": "The default value of the parameter.",
"type_view": "View of the parameter's annotation type.",
"has_annotation": (
"Whether the parameter had an annotation or not. Lack of an annotation implies "
"`TypeView(Any)`, but that is distinct from being explicitly `Any` annotated."
),
}

def __init__(self, name: str, type_view: TypeView = _any_type_view, *, default: Any | EmptyType = Empty) -> None:
def __init__(
self,
name: str,
type_view: TypeView = _any_type_view,
*,
default: Any | EmptyType = Empty,
has_annotation: bool = True,
) -> None:
self.name: Final = name
self.type_view: Final = type_view
self.default: Final = default
self.has_annotation: Final = has_annotation

def __eq__(self, other: object) -> bool:
if not isinstance(other, ParameterView):
return False

return bool(self.name == other.name and self.type_view == other.type_view and self.default == other.default)
return bool(
self.name == other.name
and self.type_view == other.type_view
and self.default == other.default
and self.has_annotation == other.has_annotation
)

def __repr__(self) -> str:
cls_name = self.__class__.__name__
Expand Down Expand Up @@ -68,5 +85,6 @@ def from_parameter(cls, parameter: Parameter, fn_type_hints: dict[str, Any]) ->
return cls(
name=parameter.name,
default=Empty if parameter.default is Signature.empty else parameter.default,
has_annotation=parameter.annotation is not Signature.empty,
type_view=TypeView(annotation),
)

0 comments on commit 483021b

Please sign in to comment.