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

adding type hinting #44

Merged
merged 13 commits into from
Apr 26, 2024
Merged

adding type hinting #44

merged 13 commits into from
Apr 26, 2024

Conversation

Gerson2102
Copy link
Contributor

Adding a lot more types to functions and parameters. There are still some errors, but I dont understand them, maybe somebody can help me.

This is the related issue: #36

@Gonmeso
Copy link
Contributor

Gonmeso commented Apr 23, 2024

Hi @Gerson2102 if you could paste the errors maybe we can offer a helping hand

@Gonmeso Gonmeso self-requested a review April 23, 2024 20:32
@Gerson2102
Copy link
Contributor Author

For sure:


giza_actions/task.py:3: error: Cannot find implementation or library stub for module named "prefect"
giza_actions/task.py:8: error: Function is missing a return type annotation
giza_actions/task.py:15: error: "None" not callable
giza_actions/utils.py:5: error: Cannot find implementation or library stub for module named "giza"
giza_actions/utils.py:6: error: Cannot find implementation or library stub for module named "giza.client"
giza_actions/model.py:4: error: Cannot find implementation or library stub for module named "giza.schemas.models"
giza_actions/model.py:5: error: Cannot find implementation or library stub for module named "giza.schemas.versions"
giza_actions/model.py:7: error: Cannot find implementation or library stub for module named "numpy"
giza_actions/model.py:8: error: Cannot find implementation or library stub for module named "onnx"
giza_actions/model.py:9: error: Cannot find implementation or library stub for module named "onnxruntime"
giza_actions/model.py:11: error: Cannot find implementation or library stub for module named "giza"
giza_actions/model.py:12: error: Cannot find implementation or library stub for module named "giza.client"
giza_actions/model.py:13: error: Cannot find implementation or library stub for module named "giza.utils.enums"
giza_actions/model.py:14: error: Cannot find implementation or library stub for module named "osiris.app"
giza_actions/model.py:282: error: Argument 2 to "_parse_cairo_response" of "GizaModel" has incompatible type "Optional[str]"; expected "str"
giza_actions/model.py:294: error: Incompatible return value type (got "str", expected "Optional[Tuple[Any, str]]")
giza_actions/model.py:423: error: Item "None" of "Optional[Any]" has no attribute "op_type"
giza_actions/deployments.py:9: error: Cannot find implementation or library stub for module named "prefect.deployments"
giza_actions/deployments.py:14: error: Function is missing a return type annotation
giza_actions/action.py:11: error: Cannot find implementation or library stub for module named "prefect"
giza_actions/action.py:11: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
giza_actions/action.py:13: error: Cannot find implementation or library stub for module named "prefect.client.schemas.schedules"
giza_actions/action.py:14: error: Cannot find implementation or library stub for module named "prefect.settings"
giza_actions/action.py:20: error: Cannot find implementation or library stub for module named "prefect.utilities.asyncutils"
giza_actions/action.py:21: error: Cannot find implementation or library stub for module named "rich.console"
giza_actions/action.py:22: error: Cannot find implementation or library stub for module named "rich.panel"
giza_actions/action.py:107: error: Cannot find implementation or library stub for module named "prefect.runner"
giza_actions/action.py:145: error: Function is missing a return type annotation
giza_actions/action.py:160: error: Function is missing a return type annotation
giza_actions/action.py:170: error: "None" not callable
giza_actions/agent.py:7: error: Module "typing" has no attribute "Self"
giza_actions/agent.py:9: error: Cannot find implementation or library stub for module named "ape"
giza_actions/agent.py:10: error: Cannot find implementation or library stub for module named "ape.contracts"
giza_actions/agent.py:11: error: Cannot find implementation or library stub for module named "ape.exceptions"
giza_actions/agent.py:12: error: Cannot find implementation or library stub for module named "ape_accounts.accounts"
giza_actions/agent.py:13: error: Cannot find implementation or library stub for module named "giza"
giza_actions/agent.py:14: error: Cannot find implementation or library stub for module named "giza.client"
giza_actions/agent.py:15: error: Cannot find implementation or library stub for module named "giza.schemas.agents"
giza_actions/agent.py:16: error: Cannot find implementation or library stub for module named "giza.schemas.jobs"
giza_actions/agent.py:17: error: Cannot find implementation or library stub for module named "giza.schemas.proofs"
giza_actions/agent.py:18: error: Cannot find implementation or library stub for module named "giza.utils.enums"
giza_actions/agent.py:84: error: Function is missing a return type annotation
giza_actions/agent.py:183: error: Argument 1 to "read_json" has incompatible type "Path"; expected "str"
giza_actions/agent.py:184: error: Incompatible types in assignment (expression has type "Dict[Any, Any]", target has type "Optional[str]")
giza_actions/agent.py:192: error: Incompatible types in assignment (expression has type "Dict[str, str]", target has type "Optional[str]")
giza_actions/agent.py:205: error: Item "None" of "Optional[str]" has no attribute "upper"
giza_actions/agent.py:207: error: Item "None" of "Optional[str]" has no attribute "upper"
giza_actions/agent.py:214: error: Function is missing a return type annotation
giza_actions/agent.py:241: error: Signature of "predict" incompatible with supertype "GizaModel"
giza_actions/agent.py:241: note:      Superclass:
giza_actions/agent.py:241: note:          def predict(self, input_file: Optional[str] = ..., input_feed: Optional[Dict[Any, Any]] = ..., verifiable: bool = ..., fp_impl: str = ..., custom_output_dtype: Optional[str] = ..., job_size: str = ..., dry_run: bool = ...) -> Optional[Tuple[Any, str]]
giza_actions/agent.py:241: note:      Subclass:
giza_actions/agent.py:241: note:          def predict(self, input_file: Optional[str] = ..., input_feed: Optional[Dict[Any, Any]] = ..., verifiable: bool = ..., fp_impl: str = ..., custom_output_dtype: Optional[str] = ..., job_size: str = ..., dry_run: bool = ..., **result_kwargs: Any) -> Union[AgentResult, Tuple[Any, str]]
giza_actions/agent.py:276: error: Incompatible return value type (got "Optional[Tuple[Any, str]]", expected "Union[AgentResult, Tuple[Any, str]]")
giza_actions/agent.py:278: error: "None" object is not iterable

All the errors that starts with "Cannot find" I ignored them.

@Gonmeso
Copy link
Contributor

Gonmeso commented Apr 24, 2024

Taking out the "library is missing stubs" ones these are left:

giza_actions/task.py:8: error: Function is missing a return type annotation
giza_actions/task.py:15: error: "None" not callable
giza_actions/model.py:282: error: Argument 2 to "_parse_cairo_response" of "GizaModel" has incompatible type "Optional[str]"; expected "str"
giza_actions/model.py:294: error: Incompatible return value type (got "str", expected "Optional[Tuple[Any, str]]")
giza_actions/model.py:423: error: Item "None" of "Optional[Any]" has no attribute "op_type"
giza_actions/deployments.py:14: error: Function is missing a return type annotation
giza_actions/action.py:11: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html\#missing-imports
giza_actions/action.py:145: error: Function is missing a return type annotation
giza_actions/action.py:160: error: Function is missing a return type annotation
giza_actions/action.py:170: error: "None" not callable
giza_actions/agent.py:7: error: Module "typing" has no attribute "Self"
giza_actions/agent.py:84: error: Function is missing a return type annotation
giza_actions/agent.py:183: error: Argument 1 to "read_json" has incompatible type "Path"; expected "str"
giza_actions/agent.py:184: error: Incompatible types in assignment (expression has type "Dict[Any, Any]", target has type "Optional[str]")
giza_actions/agent.py:192: error: Incompatible types in assignment (expression has type "Dict[str, str]", target has type "Optional[str]")
giza_actions/agent.py:205: error: Item "None" of "Optional[str]" has no attribute "upper"
giza_actions/agent.py:207: error: Item "None" of "Optional[str]" has no attribute "upper"
giza_actions/agent.py:214: error: Function is missing a return type annotation
giza_actions/agent.py:241: error: Signature of "predict" incompatible with supertype "GizaModel"
giza_actions/agent.py:241: note:      Superclass:
giza_actions/agent.py:241: note:          def predict(self, input_file: Optional[str] = ..., input_feed: Optional[Dict[Any, Any]] = ..., verifiable: bool = ..., fp_impl: str = ..., custom_output_dtype: Optional[str] = ..., job_size: str = ..., dry_run: bool = ...) -> Optional[Tuple[Any, str]]
giza_actions/agent.py:241: note:      Subclass:
giza_actions/agent.py:241: note:          def predict(self, input_file: Optional[str] = ..., input_feed: Optional[Dict[Any, Any]] = ..., verifiable: bool = ..., fp_impl: str = ..., custom_output_dtype: Optional[str] = ..., job_size: str = ..., dry_run: bool = ..., **result_kwargs: Any) -> Union[AgentResult, Tuple[Any, str]]
giza_actions/agent.py:276: error: Incompatible return value type (got "Optional[Tuple[Any, str]]", expected "Union[AgentResult, Tuple[Any, str]]")
giza_actions/agent.py:278: error: "None" object is not iterable

I think that besides this one:

giza_actions/agent.py:241: error: Signature of "predict" incompatible with supertype "GizaModel"
giza_actions/agent.py:241: note:      Superclass:
giza_actions/agent.py:241: note:          def predict(self, input_file: Optional[str] = ..., input_feed: Optional[Dict[Any, Any]] = ..., verifiable: bool = ..., fp_impl: str = ..., custom_output_dtype: Optional[str] = ..., job_size: str = ..., dry_run: bool = ...) -> Optional[Tuple[Any, str]]
giza_actions/agent.py:241: note:      Subclass:
giza_actions/agent.py:241: note:          def predict(self, input_file: Optional[str] = ..., input_feed: Optional[Dict[Any, Any]] = ..., verifiable: bool = ..., fp_impl: str = ..., custom_output_dtype: Optional[str] = ..., job_size: str = ..., dry_run: bool = ..., **result_kwargs: Any) -> Union[AgentResult, Tuple[Any, str]]
giza_actions/agent.py:276: error: Incompatible return value type (got "Optional[Tuple[Any, str]]", expected "Union[AgentResult, Tuple[Any, str]]")

Others can be tackle as well, some of them require code changes and not just typing, for example giza_actions/agent.py:205: error: Item "None" of "Optional[str]" has no attribute "upper" basically requires to make a check before doing the upper() method.

# this would give a similar error
# word type hint is: Optional[str]
word.upper()

# Adding a check that word is none should fix the error
if word is not None:
    word.upper()
else:
    # Here we could handle if word = None, maybe raising an exception if necessary
    ...

Also, @Gerson2102 make sure to update your fork as it seems to be missing the latest changes, making the CI fail.

@Gonmeso Gonmeso self-assigned this Apr 24, 2024
@Gerson2102
Copy link
Contributor Author

I dont know why the CI keep failing. I did a git rebase and everything should be good.

Copy link
Contributor

@Gonmeso Gonmeso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old agents.py functions should be handled as this is old behaviour and it needs to be improved. With the updates it might have been done along the way. This needs to be fixed for the PR to be merged.

Pre-commit check is failing in the CI check

Make sure to install and run pre-commit prior to comitting the changes.

giza_actions/agent.py Outdated Show resolved Hide resolved
giza_actions/agent.py Outdated Show resolved Hide resolved
giza_actions/agent.py Show resolved Hide resolved
@Gerson2102
Copy link
Contributor Author

Gerson2102 commented Apr 24, 2024

Right now, I'm just getting this error: giza_actions/agent.py:7: error: Module "typing" has no attribute "Self" which is weird because when I tried to import it, it suggests me that attribute. And searching on internet that is the way of how to import that attribute from typing according to this resource: https://realpython.com/python-type-self/ and I'm using python 3.11.9

@Gonmeso
Copy link
Contributor

Gonmeso commented Apr 25, 2024

Dont worry about this you have done a great job, we need the CI to pass and some simple things

Copy link
Contributor

@Gonmeso Gonmeso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost done!

We need to make some changes regarding some code changes and running pre-commit, once this is done it will be go to go 🚀

giza_actions/agent.py Outdated Show resolved Hide resolved
giza_actions/agent.py Outdated Show resolved Hide resolved
giza_actions/agent.py Show resolved Hide resolved
giza_actions/model.py Outdated Show resolved Hide resolved
@@ -275,7 +278,7 @@ def predict(
output_dtype = self._get_output_dtype()
else:
output_dtype = custom_output_dtype

output_dtype = output_dtype or "DefaultType"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that shi change is not needed, as this is handled in the if else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get it. But if i change it, we will have another error.

Copy link
Contributor

@Gonmeso Gonmeso Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let this error be I'll handle it. Remove this line and the PR will be merged as CI is passing 🥳

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let this error be I'll handle it. Remove this line and the PR will be merged as CI is passing 🥳

Finally haha!

giza_actions/agent.py Show resolved Hide resolved
@Gonmeso
Copy link
Contributor

Gonmeso commented Apr 25, 2024

CI tests fails due to circular imports, this is due to importing classes just for type checking.

Check this https://mypy.readthedocs.io/en/stable/runtime_troubles.html#import-cycles

@Gerson2102
Copy link
Contributor Author

@Gonmeso Lets see if we are done here.

@Gonmeso
Copy link
Contributor

Gonmeso commented Apr 26, 2024

Incredible work @Gerson2102 ! LGTM!

@Gonmeso Gonmeso merged commit 40dec98 into gizatechxyz:main Apr 26, 2024
1 check passed
@Gonmeso Gonmeso mentioned this pull request Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants