
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
immutable_default_args
Advanced tools
Very small package to automatically safeguard mutable function arguments, preventing them from being modified.
.. image:: https://img.shields.io/pypi/v/immutable-default-args.svg :target: https://pypi.python.org/pypi/immutable-default-args :alt: PyPI Package
.. image:: https://img.shields.io/pypi/pyversions/immutable-default-args.svg :target: https://pypi.python.org/pypi/immutable-default-args :alt: PyPI Python Versions
.. image:: https://img.shields.io/pypi/l/immutable-default-args.svg :target: https://pypi.python.org/pypi/immutable-default-args :alt: PyPI Package License
.. image:: https://travis-ci.org/timmwagener/immutable_default_args.svg?branch=develop :target: https://travis-ci.org/timmwagener/immutable_default_args :alt: Current build status for Travis CI
.. image:: https://ci.appveyor.com/api/projects/status/l67sbo0uis1kyxe9?svg=true :target: https://ci.appveyor.com/project/timmwagener/immutable-default-args :alt: Current build status for AppVeyor
.. image:: https://landscape.io/github/timmwagener/immutable_default_args/develop/landscape.svg?style=flat :target: https://landscape.io/github/timmwagener/immutable_default_args/develop :alt: Code Health
This module provides facilities for turning mutable default function arguments
into immutable ones. It is fairly lightweight and has no non-standard dependencies.
You can install this package with the standard pip
command::
$ pip install immutable_default_args
The issue with mutable argument default values <http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument>
_ is pretty well known in Python.
Basically mutable default values are assigned once at define time and can then
be modified within the function body which might come as a surprise.
Here is the example from the stackoverfow <http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument>
_ thread::
def foo(a=[]):
a.append(5)
return a
>>> foo()
[5]
>>> foo()
[5, 5]
>>> foo()
[5, 5, 5]
...
The default way of preventing this behaviour is to use None
as the default
and check for it in the function body, like so::
def foo(a=None):
a = a if (type(a) is list) else []
a.append(5)
return a
>>> foo()
[5]
>>> foo()
[5]
...
This package aims to offer two additional options to fix this issue:
@fix_mutable_kwargs
to fix a certain function.ImmutableDefaultArguments
to fix all methods, classmethods and staticmethods at once.Using the decorator::
from immutable_default_args import fix_mutable_kwargs
@fix_mutable_kwargs
def foo(a=[]):
a.append(5)
return a
>>> foo()
[5]
>>> foo()
[5]
...
It doesn't matter if the iterable is empty or not::
@fix_mutable_kwargs
def foo(a=[1, 2, {'key': 'value'}, 3, 4]):
a.append(5)
return a
>>> foo()
[1, 2, {'key': 'value'}, 3, 4, 5]
>>> foo()
[1, 2, {'key': 'value'}, 3, 4, 5]
...
Fixing all mutable default values for all methods of an object via the
ImmutableDefaultArguments
metaclass::
class Foo(object):
__metaclass__ = ImmutableDefaultArguments # Py2 syntax
def foo(self, a=[]):
a.append(5)
return a
@classmethod # staticmethods work as well
def foo_classmethod(cls, a=[]):
a.append(5)
return a
instance_of_foo = Foo()
>>> instance_of_foo.foo()
[5]
>>> instance_of_foo.foo()
[5]
...
>>> Foo.foo_classmethod()
[5]
>>> Foo.foo_classmethod()
[5]
The immutable_default_args
package is tested against Py2/3 and is supported
from Py2.7 upstream.
0.0.8 (08.05.2016)
0.0.7 (08.05.2016)
0.0.5 (08.05.2016)
0.0.2 (08.05.2016)
@fix_mutable_kwargs
decorator0.0.1 (08.05.2016)
ImmutableDefaultArguments
metaclassYou are free to do whatever you like with the code. Please note that I am not
accountable for anything that might have happened as a result of executing the
code from the immutable_default_args
package....ever.
FAQs
Very small package to automatically safeguard mutable function arguments, preventing them from being modified.
We found that immutable_default_args demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.