Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Decimal #48

Open
intellisense opened this issue Dec 22, 2020 · 2 comments
Open

Support for Decimal #48

intellisense opened this issue Dec 22, 2020 · 2 comments

Comments

@intellisense
Copy link

Hi! Any way to make this work when doing decimal.Decimal(Proxy) e.g.

import decimal
import lazy_object_proxy

s = eval("decimal.Decimal(a)", {"decimal": decimal}, {"a": lazy_object_proxy.Proxy(lambda : 10)})
print(s)

produces an error:

TypeError: conversion from Proxy to Decimal is not supported
@intellisense
Copy link
Author

Any help on this please. Thanks!

@ionelmc
Copy link
Owner

ionelmc commented Mar 4, 2021

I've checked the cpython 3 source code - the decimal module is so optimized that the instance creation runs completely in C code (no way to run any pure python code during that phase).

Unfortunately it looks like the solution is to apply a monkeypatch as early as possible in your project:

def FriendlyDecimal(value, original=decimal.Decimal):
    if isinstance(value, lazy_object_proxy.Proxy):
        return original(value.__wrapped__)
    else:
        return original(value)
decimal.Decimal = FriendlyDecimal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants