Skip to content

Explicit Any check for TypeGuard #12049

Open
@hmc-cs-mdrissi

Description

@hmc-cs-mdrissi

Bug Report

Explicit Any check does not examine the type argument of a typeguard. An example is,

def foo(x: object) -> TypeGuard[Any]:
  ...

when run with mypy --disallow-explicit-any raises no error.

Expected Behavior

I would expect 1 type error on the function definition line.

Your Environment

Using master mypy.

I'm debugging this as the issue was brought up in this pr, that TypeGuardType currently does not implement accept. If it did check TypeGuard type for explicit Any it would end up crashing. So there's two fixes needed here, 1. Check typeguard argument, and 2. Add an accept.

The function signature is checked for Any, but it happens after normalization of typeguard to bool. By the time the Any check occurs on this line while the function signature looks like this,

def (_mypy_marker: Union[builtins.int, builtins.str]) -> TypeGuard[Any]

but if you check ret_type of typ object it shows bool.

My guess is a fix could be done around here,

def anal_type_guard_arg(self, t: UnboundType, fullname: str) -> Optional[Type]:
when typeguard argument get analyzed. Alternatively normalization to bool could be avoided, but removing normalization is probably a bigger change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-disallow-anyThe disallow-any-* family of flagstopic-typeguard-typeisTypeGuard / TypeIs / PEP 647 / PEP 742

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions