Generate Python type stub - allowing more code completion in IDE#74
Generate Python type stub - allowing more code completion in IDE#74bact wants to merge 17 commits intoJPEWdev:mainfrom
Conversation
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Fix ruff, flake8, and black warning in the generated Python code (output.py in test) Fix Flake8 warning on "F824: name is never assigned in scope" by removing unnecessary global/nonlocal declarations. Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
With improved type hints some ignores are no longer needed Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
With the improved type hints and cast, the test now passed pyrefly check Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
To allow Jupyter/IPython notebook to see types, we need to keep annotations out of the block Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
|
For code autocompletion, may need to implements two things:
Can't do default assignment to class variables because it will interfere with the existing |
| return (iri for iri, _ in self.context) | ||
|
|
||
| def encode(self, encoder: Encoder, value: T_PropValue, state: EncodeState) -> None: | ||
| encoder.write_iri(str(value)) |
There was a problem hiding this comment.
This class (IRIProp) is intended to be abstract, which is why it didn't implement these; are they needed for typechecking for some reason?
| def __eq__(self, other: Any) -> bool: | ||
| if isinstance(other, ListProxy): | ||
| return self.__data == other.__data | ||
| return bool(self.__data == other.__data) |
There was a problem hiding this comment.
The bool() suprises me; I would have though a comparison operator would implicitly be a bool
There was a problem hiding this comment.
I though so as well. I'm actually trying to get rid of this because it's quite ugly....
Looks like mypy is being very defensive, as == can be override with __eq__ function and that new function can return anything.
Btw, now successfully removed few # type: ignore and one remaining pytest xfail. The code is sort of working now, but there are few places that look very clumsy. I will trying to clean that up.
There was a problem hiding this comment.
Have update the code to try to convert Collection to a list, and None to an empty list, and compare. -- Not sure if it's a good idea.
What I'm trying to see here is if we provide enough isinstance checks, will it give enough information for the type checker to let us go.
I will tinkering around this for a bit.
There was a problem hiding this comment.
Maybe not a good idea to convert Collection to list, as list suggests order.
Will revert that.
| return bool(self.__data == other.__data) | ||
|
|
||
| return self.__data == other | ||
| return bool(self.__data == other) |
| v = self.prop.decode(val_d, state) | ||
| self.prop.validate(v) | ||
| data.append(v) | ||
| if v is not None: |
There was a problem hiding this comment.
hmm, is this because decode() returns Optional[...]?
| v = prop.decode(prop_d, state) | ||
| prop.validate(v) | ||
| self.__dict__["_obj_data"][iri] = v | ||
| if prop_d is not None: |
| return (obj, "", "", "") | ||
| return ( | ||
| obj._id or "", | ||
| getattr(obj, "_id", None) or "", |
| # This covers custom extensions | ||
| reg_type(obj.TYPE, obj.COMPACT_TYPE, obj, True) | ||
| reg_type( | ||
| str(obj.TYPE), |
| reg_type(obj.TYPE, obj.COMPACT_TYPE, obj, True) | ||
| reg_type( | ||
| str(obj.TYPE), | ||
| str(obj.COMPACT_TYPE) if obj.COMPACT_TYPE else None, |
Generate stub (.pyi) for type annotations Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Convert different types to list and compare Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
|
This PR is probably getting too big, maybe it's better to break it down.
Will wait for #76 to get merged (it will changed structure of how we maintain class properties) and see where to work first. |
Sounds good. #76 is the last big change I have planned; there will be a few smaller ones after but they shouldn't interfere too much |
This PR improves type annotations and adds Python type stub (.pyi) generation. This will allow more code completion in IDE.
Before this PR, only few class members are shown in autocompletion list:
With this PR, more class members, including ones from its superclasses, are shown:
(example from spdx-python-model -- need spdx/spdx-python-model#18 to copy the type stub to bindings path)
--
Improve type annotations in the generated Python binding:
Generic[T_PropValue]toPropertyandListProxyclasses to allow them to define specific types, reduce the use ofAny.DATATYPE_PYTHON_ALIASESmapping)setwithtyping.Setto maintain Python 3.8 compatibilityFix lint warnings in the generated code:
global/nonlocalwhere the variable is not being modified locally, to fix F824 Flake8 warning in the generated Python code.obj_datavariable inSHACLExtensibleObject.__setitem__to fixF841warning in Ruff--
They generated type stub will look like below.
Generated "link-class" from the test model:
The stub for that class: