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

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

Open
3 of 10 tasks
mattip opened this issue Aug 15, 2024 · 4 comments
Open
3 of 10 tasks

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

mattip opened this issue Aug 15, 2024 · 4 comments

Comments

@mattip
Copy link

mattip commented Aug 15, 2024

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

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?

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

  • Spyder version: 5.5.5
  • Python version: 3.8
  • Qt version: 5.15.2
  • PyQt version: 5.15.11
  • Operating System name/version: Windows 10

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)

@mattip mattip changed the title Show repr() info on a WindowPath variable ENH: Show repr() info on a WindowPath variable Aug 15, 2024
@PhilipYip1988
Copy link

PhilipYip1988 commented Aug 15, 2024

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
Copy link
Author

mattip commented Aug 16, 2024

Extra points for doing the same with SimpleNamespace

@dalthviz
Copy link
Member

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 ?

@PhilipYip1988
Copy link

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

@dalthviz dalthviz added this to the wishlist milestone Nov 6, 2024
@dalthviz dalthviz removed their assignment Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants