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

pprinting a Dataclass with a numpy array field and None as default fails #94

Open
aseltmann opened this issue Apr 12, 2023 · 0 comments

Comments

@aseltmann
Copy link

aseltmann commented Apr 12, 2023

  • PrettyPrinter version: 0.18.0
  • Python version: 3.9.16 (main, Dec 7 2022, 01:11:51) [GCC 9.4.0]
  • Operating System: uname_result(system='Linux', node='73b98b6d221c', release='5.10.147+', version='# 1 SMP Sat Dec 10 16:00:40 UTC 2022', machine='x86_64')

Description

I use dataclasses with numpy array fields and noticed that prettyprinting fails when the typing for init values is e.g. Optional[np.ndarray] or Union[np.ndarray, None] and additionally None is specified as default.

  • The same did not happen e.g. for Optional[List] = None.
  • This did not happen for Union[np.ndarray, str] = 'test'
  • The system I use this on is a Google Colab (see info above), which currently still runs Python 3.9 and might have additional specifics I don't know about.

What I Did

Input:

import numpy as np

from dataclasses import dataclass
from typing import Optional
from prettyprinter import pprint, install_extras
install_extras()

@dataclass
class Test1:
    another_array: np.ndarray
    maybe_array: Optional[np.ndarray]

@dataclass
class Test2:
    another_array: np.ndarray
    maybe_array: Optional[np.ndarray] = None

array = np.array([1, 2, 3, 4])

pprint(Test1(maybe_array=array, another_array=array))
print('---')
pprint(Test2(maybe_array=array, another_array=array))

Output:

Test1(
    another_array=numpy.ndarray([1, 2, 3, 4]),
    maybe_array=numpy.ndarray([1, 2, 3, 4])
)
---
Test2(another_array=array([1, 2, 3, 4]), maybe_array=array([1, 2, 3, 4]))
/usr/local/lib/python3.9/dist-packages/prettyprinter/prettyprinter.py:340: UserWarning: The pretty printer for Test2, prettyprinter.prettyprinter._repr_pretty, raised an exception. Falling back to default repr.

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/prettyprinter/prettyprinter.py", line 387, in _run_pretty
    doc = pretty_fn(value, ctx)
  File "/usr/local/lib/python3.9/dist-packages/prettyprinter/prettyprinter.py", line 424, in _repr_pretty
    return fn(value, ctx)
  File "/usr/local/lib/python3.9/dist-packages/prettyprinter/extras/dataclasses.py", line 40, in pretty_dataclass_instance
    if field_def.default != getattr(value, field_def.name):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

  warnings.warn(

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

1 participant