spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.34k stars 1.62k forks source link

ENH: Show repr() info on a WindowPath variable #22351

Open mattip opened 3 months ago

mattip commented 3 months ago

Issue Report Checklist

Problem Description

When I look at the value of a PathLib WindowsPath variable, it only shows "WindowsPath object of Pathlib moduled" and not the repr() of the pathlib Screenshot 2024-08-15 105827

What steps reproduce the problem?

1.

What is the expected output? What do you see instead?

I would like to see the path

Paste Traceback/Error Below (if applicable)


PASTE TRACEBACK HERE

Versions

Dependencies

# Mandatory:
atomicwrites >=1.2.0          :  1.4.1 (OK)
chardet >=2.0.0               :  5.2.0 (OK)
cloudpickle >=0.5.0           :  3.0.0 (OK)
cookiecutter >=1.6.0          :  2.6.0 (OK)
diff_match_patch >=20181111   :  20230430 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=8.12.2,<8.13.0      :  8.12.3 (OK)
jedi >=0.17.2,<0.20.0         :  0.19.1 (OK)
jellyfish >=0.7               :  1.1.0 (OK)
jsonschema >=3.2.0            :  4.23.0 (OK)
keyring >=17.0.0              :  25.3.0 (OK)
nbconvert >=4.0               :  7.16.4 (OK)
numpydoc >=0.6.0              :  1.7.0 (OK)
paramiko >=2.4.0              :  3.4.1 (OK)
parso >=0.7.0,<0.9.0          :  0.8.4 (OK)
pexpect >=4.4.0               :  4.9.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  6.0.0 (OK)
pygments >=2.0                :  2.18.0 (OK)
pylint >=3.1,<4               :  3.2.6 (OK)
pylint_venv >=3.0.2           :  3.0.3 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.11.0,<1.12.0        :  1.11.0 (OK)
pylsp_black >=2.0.0,<3.0.0    :  2.0.0 (OK)
qdarkstyle >=3.2.0,<3.3.0     :  3.2.3 (OK)
qstylizer >=0.2.2             :  0.2.3 (OK)
qtawesome >=1.3.1,<1.4.0      :  1.3.1 (OK)
qtconsole >=5.5.1,<5.6.0      :  5.5.2 (OK)
qtpy >=2.1.0                  :  2.4.1 (OK)
rtree >=0.9.7                 :  1.3.0 (OK)
setuptools >=49.6.0           :  56.0.0 (OK)
sphinx >=0.6.6                :  7.1.2 (OK)
spyder_kernels >=2.5.2,<2.6.0 :  2.5.2 (OK)
textdistance >=4.2.0          :  4.6.2 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  4.0.2 (OK)
zmq >=24.0.0                  :  26.1.0 (OK)

# Optional:
cython >=0.21                 :  None (NOK)
matplotlib >=3.0.0            :  3.7.5 (OK)
numpy >=1.7                   :  1.24.4 (OK)
pandas >=1.1.1                :  None (NOK)
scipy >=0.17.0                :  1.10.1 (OK)
sympy >=0.7.3                 :  None (NOK)
PhilipYip1988 commented 3 months ago

For Path, there are a few different forms, the repr, the str and the printed version of the str with the escape characters processed.

from pathlib import Path
documents = Path.home()

repr(documents)

str(documents)

print(str(documents))

The repr has the advantage as it gives a clear indicator in how to instantiate a new Path.

However displaying the printed version of the str form, where the escape characters are processed has the advantage in that it matches what is typically displayed in the Operating System and is consistent to the file paths seen elsewhere in Spyder:

Screenshot 2024-08-15 231534

It would also be useful if the variable explorer supported editing a PathLib instance, similar to the way it does for a str instance. This would allow a path to be pasted into the field from Windows Explorer:

Screenshot 2024-08-15 233136

Screenshot 2024-08-15 233342

mattip commented 3 months ago

Extra points for doing the same with SimpleNamespace

dalthviz commented 2 months ago

Hi @mattip and @PhilipYip1988 thank you for the feedback and ideas! I think improving support for pathlib.Path over the Variable Explorer makes sense :+1:

Checking I think there was a similar issue to this one (#12683). Maybe could it be worthy to address pathlib.Path support although a more general approach to add support for custom types needs to be done (https://github.com/spyder-ide/spyder/issues/7092 and https://github.com/spyder-ide/spyder/issues/6338)? 🤔

What do you think @spyder-ide/core-developers ?

PhilipYip1988 commented 2 months ago

Hi @mattip and @PhilipYip1988 thank you for the feedback and ideas! I think improving support for pathlib.Path over the Variable Explorer makes sense 👍

Checking I think there was a similar issue to this one (#12683). Maybe could it be worthy to address pathlib.Path support although a more general approach to add support for custom types needs to be done (#7092 and #6338)? 🤔

What do you think @spyder-ide/core-developers ?

Not too sure how easy it is to implement, but perhaps Spyder can copy the behaviour of the JupyterLab Variable Inspector for datatypes that are not fully supported by Spyder? i.e. variables that can only be viewed and not explored/modified graphically.

The formal representation for datatypes is a bit more useful than "object or ... module."

jupyterlab

spyder

For example some relatively common builtins classes frozenset and bytearray, the collections module classes and the datetime64 and timedelta64 classes from numpy:

from pathlib import Path
from collections import defaultdict, deque, Counter 
from collections import namedtuple, UserString, UserDict, UserList
import numpy as np

unique = frozenset({1, 2, 3, 4, 5})
barray = bytearray(b'hello')
default = defaultdict(str, {'a': '', 'b': '', 'c': ''})
counts = Counter(('hello world!'))
double_ended_queue = deque([1, 2, 3, 4, 5], 7)

DateTuple = namedtuple('DateTuple', ['day', 'month', 'year'], 
                       defaults=(26, 8, 2024))

day1 = DateTuple(day=29)

text = UserString('hello world!')

class CustomText(UserString):
    def center_and_upper(self):
        return self.upper().center(20, '#')

text2 = CustomText('hello')

text2.center_and_upper()

windows = Path(r'C:\Windows')

day2 = np.datetime64('2024-08-26')

timediff1 = np.timedelta64(12, 'h') + np.timedelta64(12, 's')

dates = np.arange(np.datetime64('2024-08-21'), 
                  np.datetime64('2024-08-26'),
                  np.timedelta64(1, 'D'))

timediffs = np.arange(np.timedelta64(0, 'h'),
                      np.timedelta64(1, 'h'),
                      np.timedelta64(10, 's'))

In Spyder some of the collections datatypes can be viewed as they are essentially treated as a subclass of a builtins... For example a namedtuple displays as a tuple, and a Counter displays as a dict... So the expected behaviour is almost there for some of these classes...

An improvement would be for the namedtuple to display the repr on the Variable Explorer matching the JupyterLab Variable Inspector. The expanded view would ideally display the field names in the order specified beside the value for each field, if this class was even further supported. #20688

For the Counter, the repr should also be used on the Variable Explorer. The expanded view should list the data by value (highest to lowest). The expanded view for the dict and defaultdict should use the insertion order #9136.

The deque should display its repr on the Variable Explorer and if given extended support should display similarly to a list.

The frozenset should display its repr on the Variable Explorer and should display like a set in the expanded view. #21858.

Clicking into the datetime64 and timedelta64 instances shows a more sensible representation, which should probably be the default behaviour. The variable explorer does not support an nd.array of these datatypes however if these are cast into a pandas Series, then they display as expected... Not sure if the behaviour of these can be updated to match the pandas Series by default. #10423

spyder3

spyder4