2

I'm trying to understand how to make this work:

def someMethod() -> dict[any, any]:
    if not os.path.exists('some path'):
        return {}

    config = {'a': 1, 'b': 2}
    return config

I don't think that's correct. Seeing this error - Declared return type, "dict[Unknown, Unknown]", is partially unknownPylance

The idea is to return empty dict if a path doesn't exist (or on some condition) or correct dict with key-value pairs.

Any ideas?

scorpion35
  • 944
  • 2
  • 12
  • 30

1 Answers1

5

Lowercase any is a Python built-in function and not a type. Instead, you have to import capital Any from the typing module.

from typing import Any
import os

def someMethod() -> dict[Any, Any]:
    if not os.path.exists('some path'):
        return {}

    config = {'a': 1, 'b': 2}
    return config
ftorre
  • 501
  • 1
  • 9
  • Since the returned dict is always a `dict[str, int]` that might be better — or even `typing.Mapping[str, int]` if the caller isn’t meant to modify it. – Samwise May 29 '23 at 16:30