gizatechxyz / giza-agents

Easy to use Verifiable AI and smart contracts interoperability.
https://docs.gizatech.xyz/products/ai-agents
MIT License
15 stars 10 forks source link

adding type hinting #44

Closed Gerson2102 closed 6 months ago

Gerson2102 commented 6 months ago

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 commented 6 months ago

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

Gerson2102 commented 6 months ago

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 commented 6 months ago

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.

Gerson2102 commented 6 months ago

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

Gerson2102 commented 6 months ago

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 commented 6 months ago

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

Gonmeso commented 6 months ago

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 commented 6 months ago

@Gonmeso Lets see if we are done here.

Gonmeso commented 6 months ago

Incredible work @Gerson2102 ! LGTM!