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

Use typing.Text instead of str for Cython defined functions #143

Closed
2 tasks done
GhislainJ opened this issue Jun 15, 2021 · 1 comment
Closed
2 tasks done

Use typing.Text instead of str for Cython defined functions #143

GhislainJ opened this issue Jun 15, 2021 · 1 comment

Comments

@GhislainJ
Copy link
Contributor

  • I'm submitting a ...

    • bug report
    • feature request
  • What is the current behavior?

Jsonschema computation fails at mypy level (get_type_hints) when usin str typing for a function (init for example) that is defined inside cython compiled module.

This is due to unicode beeing used as typing (cython is running with Python2 ?) and failing in typing.get_type_hints accordingly to this discussion :

python/typing#208

This issue is a bit harsh but it does basically say that unicode type (py2) and str (py3) are incompatible. Instead typing.Text is a type alias that checks for Union[str, unicode]

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem Avoid reference to other packages
import volmdlr
from typing import get_type_hints
print(volmdlr.Point3D.__init__.__annotations__)
get_type_hints(volmdlr.Point3D.__init__)

Traceback :

File "/home/jezequel/Documents/Dessia/git/dessia-platform-backend/dessia_platform_backend/routes/classes.py", line 54, in get_jsonschema
    jsonschema = class_.jsonschema()
  File "/home/jezequel/Documents/Dessia/git/dessia-common/dessia_common/core.py", line 340, in jsonschema
    annotations = get_type_hints(init)
  File "/usr/lib/python3.9/typing.py", line 1442, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/usr/lib/python3.9/typing.py", line 277, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/usr/lib/python3.9/typing.py", line 533, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>

  • What is the expected behavior?

  • What is the motivation / use case for changing the behavior?

Good jsonchema computation of volmdlr.core_compiled.pyx classes. At the moment it breaks on platform side :

https://gitea.dessia.tech/DessiA/frontend/issues/394

  • Possible fixes

Replace every str typing by typing.Text as for Point3D :

class Point3D(Vector3D):
    _standalone_in_db = False

    def __init__(self, x: float, y: float, z: float, name: Text=''):
        Vector3D.__init__(self, x, y, z, name)

This fixes the issue. I opened a branch with the example for Point3D if we want to implement this fix

image

  • Please tell us about your environment:

    • branch: dev
    • commit: 68c81b5
    • fix branch: text-typing
    • commit: b193481
    • python version: 3.9
@masfaraud
Copy link
Contributor

masfaraud commented Jun 15, 2021

Fixed on dev c3501c9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants