Skip to content

Question about type signature for "don't know" #3712

Closed

Description

@thanatos

This is presently just a question, but perhaps it is a feature request.

As I'm annotating functions in a pre-existing codebase, I have found that I tend to have two distinct things that I'd like to annotate:

  • This function takes objects of any type as input
  • This function takes objects of a particular type as input, but I have absolutely no idea what that type is.

mypy has the Any type; presently, my assumption is that this is intended for the first bullet. I've been abusing it for the second bullet as well, but this means that if I come back to the signature later, I have no idea if the Any means "anything" or "dunno".

Does mypy have a better "Unknown" type? Would it be recommended to just say,

Unknown = Any

?

(I think most ideally, I could, perhaps with a flag like --flag-unknowns, ask mypy to emit warnings about uses of something like "Unknown".)

(In case you're wondering/thinking "just don't annotate it for the unknown case", I try to do that, but this mostly comes up with dicts where I know the key's type (usually, str) but not the values — e.g. Dict[str, ???], or in functions, where I know some of the args but not all of the arguments — e.g. (int, str, str, str, ?, ??, ???) -> Thing —, and I'd like to document and ideally get mypy's type-checking support for what I know.)

Activity

  1. gvanrossum commented on Jul 13, 2017

    @gvanrossum
    Member

    I guess it depends on what your workflow is. For your workflow I recommend just using the Unknown = Any alias. Also note that in some case it's fine to use object instead of Any -- this essentially means that if you want to do anything it (except something like str()) you have to use isinstance() to check the type. For dicts, I also recommend checking out TypedDict in the docs.

  2. ilevkivskyi commented on Jul 13, 2017

    @ilevkivskyi
    Member

    mypy has the Any type; presently, my assumption is that this is intended for the first bullet

    The first bullet is actually object. And Any is for the second one.

  3. gvanrossum commented on Jul 13, 2017

    @gvanrossum
    Member

    The first bullet is actually object. And Any is for the second one.

    IMO that depends -- in stubs this is certainly true, but in implementation code, using object often means you get errors everywhere you use it inside the function.

  4. bluetech commented on Oct 24, 2018

    @bluetech
    Contributor

    If in Python/mypy object is equivalent to unknown (is object absolutely a top type in Python? Does it need any special handling in mypy?), then I think this issue can be closed since that already exists.


    Some random comments:

    TypeScript has an unknown top type for this: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type

    For example, currently json.loads is annotated with -> Any, so json.loads('5') + 'foo' type checks. But if it is changed to -> Unknown, then it is not possible to do anything with the return value except narrow it down with is or isinstance, so it's safe.

    (Disclaimer: I don't actually think it's a good tradeoff to change json.loads to return Unknown/object, but it's a nice illustration for how this type can be useful. Also for JSON specifically it's possible to have a more precise recursive type Json = Union[None, bool, int, float, str, List[Json], Dict[str, Json]], so perhaps pickle is a better example).

  5. gvanrossum commented on Oct 24, 2018

    @gvanrossum
    Member

    Yeah, object is absolutely the top type in mypy. Closing.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions