question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What are the recommended type annotations?

See original GitHub issue

Suppose I have this:

from munch import munchify


def gen_dict():
    return munchify({'foo': 'bar', 'id': 123})    

What is the recommended way to type annotate the return value of gen_dict()? Simply munch.Munch?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
d1618033commented, Sep 3, 2020

See https://github.com/python/mypy/issues/6701#issuecomment-485017283v

The reason is presumably that mypy can’t find stubs for bson, so it treats all names from it as Any, and it disallows using NewType on Any. I agree that this would be useful to mention in the docs.

1reaction
d1618033commented, Sep 3, 2020

The issue is that there’s no type hinting in the munch code (and I am unsure if we should add right now). But in the mean time you can use this stub file:

from typing import Any, Optional, Callable


class Munch(dict):
    def __init__(self, *args: Any, **kwargs: Any) -> None: ...
    def __getattr__(self, k: str) -> Any: ...
    def __setattr__(self, k: str, v: Any) -> None: ...
    def __delattr__(self, k: str) -> None: ...
    def toDict(self) -> dict: ...
    @property
    def __dict__(self): ...  # type: ignore
    def __dir__(self) -> list: ...
    __members__: Any = ...
    @classmethod
    def fromDict(cls, d: dict) -> "Munch": ...
    def copy(self) -> "Munch": ...
    def update(self, *args: Any, **kwargs: Any) -> None: ...
    def get(self, k: str, d: Optional[Any] = ...) -> Any: ...
    def setdefault(self, k: str, d: Optional[Any] = ...) -> Any: ...

class AutoMunch(Munch):
    def __setattr__(self, k: str, v: Any) -> None: ...

class DefaultMunch(Munch):
    __default__: Any = ...
    def __init__(self, *args: Any, **kwargs: Any) -> None: ...
    def __getattr__(self, k: str) -> Any: ...
    def __setattr__(self, k: str, v: Any) -> None: ...
    def __getitem__(self, k: str) -> Any: ...
    @classmethod
    def fromDict(cls, d: dict, default: Optional[Any] = ...) -> "Munch": ...
    def copy(self) -> "Munch": ...

class DefaultFactoryMunch(Munch):
    default_factory: Callable[[],Any] = ...
    def __init__(self, default_factory: Callable[[],Any], *args: Any, **kwargs: Any) -> None: ...
    @classmethod
    def fromDict(cls, d: dict, default_factory: Callable[[],Any]) -> "Munch": ...  # type: ignore
    def copy(self) -> "Munch": ...
    def __setattr__(self, k: str, v: Any) -> None: ...
    def __missing__(self, k: str) -> Any: ...

def munchify(x: dict, factory: Any = ...) -> Munch: ...
def unmunchify(x: Munch) -> dict: ...

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding type annotation in Python - LogRocket Blog
In this extensive post with specific examples, learn how to use Python type annotation to your advantage using the mypy library.
Read more >
Type Annotation in Python | Towards Data Science
Type annotations — also known as type signatures — are used to indicate the datatypes of variables and input/outputs of functions and ...
Read more >
Using Python's Type Annotations - DEV Community ‍ ‍
Type annotations and hints are incredibly useful for teams and multi-developer Python applications. It removes most of the guesswork from ...
Read more >
Pros and Cons of Type Hints - Real Python
Type hints work best in modern Pythons. Annotations were introduced in Python 3.0, and it's possible to use type comments in Python 2.7....
Read more >
PEP 484 – Type Hints - Python Enhancement Proposals
It is recommended but not required that checked functions have annotations for all arguments and the return type. For a checked function, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found