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 type guards #183

Closed
helderco opened this issue Nov 1, 2022 · 3 comments
Closed

Use type guards #183

helderco opened this issue Nov 1, 2022 · 3 comments
Assignees
Labels
enhancement Enhancement request
Milestone

Comments

@helderco
Copy link

helderco commented Nov 1, 2022

As an example I specifically need them in type/definition.py to correctly narrow down the types in a codegen tool. In the meantime I've reimplemented these functions:

def is_required_type(t: Any) -> TypeGuard[GraphQLNonNull]:
    return isinstance(t, GraphQLNonNull)


def is_list_type(t: Any) -> TypeGuard[GraphQLList]:
    return isinstance(t, GraphQLList)


def is_wrapping_type(t: Any) -> TypeGuard[GraphQLWrappingType]:
    return isinstance(t, GraphQLWrappingType)


def is_scalar_type(t: Any) -> TypeGuard[GraphQLScalarType]:
    return isinstance(t, GraphQLScalarType)


def is_input_object_type(t: Any) -> TypeGuard[GraphQLInputObjectType]:
    return isinstance(t, GraphQLInputObjectType)


def is_object_type(t: Any) -> TypeGuard[GraphQLObjectType]:
    return isinstance(t, GraphQLObjectType)

I thought the isinstance would be enough for the type checker to infer in these cases but if the return type is bool in these functions they're not narrowed.

@Cito
Copy link
Member

Cito commented Nov 1, 2022

Thanks for reminding me of this feature @helderco, we should definitely do that. Though it's only available since Python 3.10, it also can be used in earlier Python versions via typing-extensions.

@Cito Cito self-assigned this Nov 1, 2022
@Cito Cito added the enhancement Enhancement request label Nov 1, 2022
@Cito Cito added this to the v3.3 milestone Nov 1, 2022
Cito added a commit that referenced this issue Nov 2, 2022
@Cito
Copy link
Member

Cito commented Nov 2, 2022

Commited to the main branch, will be available in the next (alpha) release of version 3.3.

@Cito Cito closed this as completed Nov 2, 2022
@Cito
Copy link
Member

Cito commented Nov 3, 2022

Available now in v3.3.0a2.

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

No branches or pull requests

2 participants