Skip to content

A collection of "None" objects compatible with various types

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING
Notifications You must be signed in to change notification settings

LLyaudet/python-none-objects

Repository files navigation

python-none-objects

PyPI-version-badge Downloads-badge Code-style:black:badge Imports:isort:badge Typecheck:mypy:badge Linting:pylint:badge CodeFactor-badge CodeClimateMaintainability-badge Codacy-badge GitHub-top-language-badge GitHub-license-badge PyPI-python-version-badge GitHub-code-size-in-bytes-badge

A collection of "None" objects
compatible with various Python types

The following code yields warning for "Default argument value is mutable".

from typing import List, Dict

def foo(some: int, other: List = [], thing: Dict = {}):
    for o in other:
        bar(some, o, thing)

It is usually recommended to use None instead (https://stackoverflow.com/questions/41686829 /why-does-pycharm-warn-about-mutable-default-arguments- how-can-i-work-around-the):

from typing import List, Dict, Optional

def foo(
    some: int,
    other: Optional[List] = None,
    thing: Optional[Dict] = None,
):
    if other is None:
        other = []
    if thing is None:
        thing = {}
    for o in other:
        bar(some, o, thing)

But I prefer less boilerplate code like this:

from typing import Iterable, Mapping
from types import MappingProxyType

def foo(
    some: int,
    other: Iterable = (),
    thing: Mapping = MappingProxyType({}),
):
    for o in other:
        bar(some, o, thing)

This package introduces constants to make the code more readable:

from typing import Iterable, Mapping
from python_none_objects import NoneIterable, NoneMapping

def foo(
    some: int,
    other: Iterable = NoneIterable,
    thing: Mapping = NoneMapping,
):
    for o in other:
        bar(some, o, thing)

Be sure to look at the discussions on GitHub: https://github.com/LLyaudet/python-none-objects/discussions.

There is a poll on the naming convention you would prefer: #2.

And there is a discussion on various ideas to optimize the code with these constants: #3.

I think it would be better to have this kind of constants in the standard library. If you think after reading everything, that it is indeed a good idea, add a star to this repository to let the rest of the Python community know that you would like to see such constant objects in the language :). https://github.com/LLyaudet/python-none-objects/ If the project gains popularity, I'll try to propose it officially.

About

A collection of "None" objects compatible with various types

Resources

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published