Skip to content

bpo-15450: Allow subclassing of filecmp.dircmp #23424

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

Merged
merged 1 commit into from
Nov 23, 2020

Conversation

NickCrews
Copy link
Contributor

@NickCrews NickCrews commented Nov 20, 2020

Currently, the subdirs attribute of filecmp.dircmp does not respect subclassing:

from filecmp import dircmp
class MyDirCmp(dircmp):
... pass
...
my_dcmp = MyDirCmp('dir1', 'dir2')
for item in my_dcmp.subdirs.values():
... print(type(item))
... break
...
<class 'filecmp.dircmp'>

This is the only place where dircmp does not respect subclassing. It can be corrected here:

def phase4(self): # Find out differences between common subdirectories
...
...
self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide)

This would let one do things like override dircmp.report() and have dircmp.report_full_closure() behave as expected, or replace the phaseN()
methods with your custom behavior and have that work for all
subdirectories.

Co-authored-by: Chris Jerdonek chris.jerdonek@gmail.com

https://bugs.python.org/issue15450

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@NickCrews

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@NickCrews
Copy link
Contributor Author

CLA signing is pending...

@NickCrews
Copy link
Contributor Author

I force pushed to fix a typo on windows for case-insensitive file names.

Currently, the subdirs attribute of filecmp.dircmp does not respect subclassing:

>>> from filecmp import dircmp
>>> class MyDirCmp(dircmp):
...   pass
...
>>> my_dcmp = MyDirCmp('dir1', 'dir2')
>>> for item in my_dcmp.subdirs.values():
...   print(type(item))
...   break
...
<class 'filecmp.dircmp'>

This is the only place where dircmp does not respect subclassing.  It can be corrected here:

def phase4(self): # Find out differences between common subdirectories
    ...
        ...
        self.subdirs[x]  = dircmp(a_x, b_x, self.ignore, self.hide)

This would let one do things like override dircmp.report() and have dircmp.report_full_closure() behave as expected, or replace the phaseN()
methods with your custom behavior and have that work for all
subdirectories.

Co-Authored-By: Chris Jerdonek <chris.jerdonek@gmail.com>
@NickCrews
Copy link
Contributor Author

Added versionchanged:: 3.10 note to docs

# these are stored in a dictionary indexed by filename.
# The hide and ignore properties are inherited from the parent
self.subdirs = {}
for x in self.common_dirs:
a_x = os.path.join(self.left, x)
b_x = os.path.join(self.right, x)
self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide)
self.subdirs[x] = self.__class__(a_x, b_x, self.ignore, self.hide)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use type(self) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really any good reason, I was copying @cjerdonek's patch and that's what he used. They evaluate to the same thing, right? I believe in Python 2 we would have needed to use .__class__, but now there is not that restriction. You're just thinking of style?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing I guess, both variants are equal.

@asvetlov asvetlov merged commit 2f2f9d0 into python:master Nov 23, 2020
@bedevere-bot
Copy link

@asvetlov: Please replace # with GH- in the commit message next time. Thanks!

@asvetlov
Copy link
Contributor

Thanks!

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
Co-authored-by: Chris Jerdonek <chris.jerdonek@gmail.com>
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

Successfully merging this pull request may close these issues.

5 participants