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

Allow type hinting #219

Open
mohankumaru opened this issue Jan 19, 2022 · 4 comments
Open

Allow type hinting #219

mohankumaru opened this issue Jan 19, 2022 · 4 comments

Comments

@mohankumaru
Copy link

mohankumaru commented Jan 19, 2022

Hi,
Is there a way to include annotated assignment in the code being executed?

               sample_code = """
               a: int = 1
                """
                supported_builtins["_getattr_"] = safer_getattr
                supported_builtins["_getiter_"] = default_guarded_getiter
                supported_builtins["_iter_unpack_sequence_"] = guarded_iter_unpack_sequence
                supported_builtins["dict"] = dict
                supported_builtins["list"] = list
                byte_code = compile_restricted(
                            sample_code,
                            filename='<inline code>',
                            mode='exec'
                            )
                exec(byte_code, {'__builtins__':supported_builtins}, {'input_arg': {"a":1})

Where I am looking to add type hints to my inline code "sample_code". It will throw exception saying " AnnAssign statements are not allowed.". Is there a way to get around this?

@d-maurer
Copy link
Contributor

d-maurer commented Jan 19, 2022 via email

@mohankumaru
Copy link
Author

@d-maurer , Thank you for quick reply.
We run mypy regularly on our codebase and there are some code which needs to be restricted.
I wanted to know if its possible through restricted python, possibly I can strip the type checks before restricted python compilation.

@icemac
Copy link
Member

icemac commented Jan 21, 2022

Currently not supporting does not mean: will never support. So I am reopening this issue and mark it as an enhancement request.

As type annotations do not influence the runtime behavior it should be no problem to support them during the parsing and remove them from the AST.

@aszs
Copy link

aszs commented Feb 6, 2024

Hi I stumbled upon this issue and wanted to add that this is trivial to implement because AnnAssign nodes only have a target field declared as Name | Attribute | Subscript and so doesn't need the logic found in visit_Assign().

User code could look like this

from RestrictedPython import RestrictingNodeTransformer

class MyNodeTransformer(RestrictingNodeTransformer):

    def visit_AnnAssign(self, node: AnnAssign) -> Any:
        return self.node_contents_visit(node)

Here's our implementation: https://github.com/onecommons/unfurl/blob/c238d00e916d7071191104dd3fab0a34aded2574/tosca-package/tosca/loader.py#L504

In Python 3.12, the following node types were added for the new type alias syntax, so you might want to add these too, they only impact type annotations so are safe at runtime:

    def visit_TypeAlias(self, node) -> Any:
        return self.node_contents_visit(node)

    def visit_TypeVar(self, node) -> Any:
        return self.node_contents_visit(node)

    def visit_TypeVarTuple(self, node) -> Any:
        return self.node_contents_visit(node)

    def visit_ParamSpec(self, node) -> Any:
        return self.node_contents_visit(node)

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

No branches or pull requests

4 participants