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

FlattenSentinel can't be used as return type annotation #1075

Open
Viicos opened this issue Dec 24, 2023 · 1 comment
Open

FlattenSentinel can't be used as return type annotation #1075

Viicos opened this issue Dec 24, 2023 · 1 comment
Labels
bug Something isn't working machinery Internal plumbing for visitor, transformer, matcher APIs

Comments

@Viicos
Copy link

Viicos commented Dec 24, 2023

Consider the following code:

MATCHER = m.FunctionDef(name=m.Name("__init__"))

class MyVisitor(...):
    ...
    @m.leave(MATCHER)
    def mutate_init(
        self,
        original_node: cst.FunctionDef,
        updated_node: cst.FunctionDef
    ) -> cst.FlattenSentinel[cst.FunctionDef]:
        ...

This raises the following exception: TypeError: issubclass() arg 1 must be a class

for ret in possible_annotated_classes:
for annotation in possible_returns:
if issubclass(ret, annotation):

Here, ret is cst.FlattenSentinel[cst.FunctionDef] (an instance of GenericAlias), annotation is libcst._nodes.statement.BaseStatement.

Works as expected without parametrizing the class

Edit: Fails at runtime as well without parametrizing the class:

libcst.matchers._visitors.MatchDecoratorMismatch: Invalid function signature for ForwardRelationOverloadCodemod.mutate_ManyToManyField_FunctionDef: @leave decorated function cannot return the type FlattenSentinel.

Tbh this runtime check of the return annotation is a bit surprising: type hints are not supposed to alter the run time behavior (except when explicitly advertised as is, e.g. like pydantic). It might be worth adding a try..except on this check, and only reraise if the exception is an instance of MatchDecoratorMismatch.

@Viicos Viicos changed the title FlattenSentinel can't be used as return type annotation when parametrized. FlattenSentinel can't be used as return type annotation Dec 24, 2023
@zsol zsol added bug Something isn't working machinery Internal plumbing for visitor, transformer, matcher APIs labels Jan 4, 2024
@zsol
Copy link
Member

zsol commented Jan 4, 2024

See #906 also for FlattenSentinel-related typing bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working machinery Internal plumbing for visitor, transformer, matcher APIs
Projects
None yet
Development

No branches or pull requests

2 participants