Skip to content

ENH: Return Path in data_path? #10320

Closed

Description

@larsoner

I saw @hoechenberger's code here:

sample_dir = mne.datasets.sample.data_path()
raw_path = Path(sample_dir) / 'MEG' / 'sample' / 'sample_audvis_raw.fif'

And thought it was a bit sad that he had to Path(...) the data_path() result.

Since 1.0 is coming out, I think one small breaking change we could make would be to return Path instead of str in data_path.

  1. It actually matches the name of the function better
  2. It should help people modernize their code a bit with things like ...data_path() / 'subject'
  3. op.join still works and returns str

The big problem I see is:

  1. This fails:
    >>> Path('.') + '/foo'
    TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
    
    The fix is pretty easy in this case, but it will break people's code. The fix is pretty easy, but it's still some pain...

We can probably get around this, though, with some class magic -- we could subclass PosixPath and WindowsPath with support for a + operator, where if the other arg is a str then we cast to str and return op.join(path_str, other_str) (after stripping op.sep from the other_str).

Thoughts?

An alternative would be to do something like data_path(..., out='path'), but I'm not sure that's much better than having people import from pathlib (though I guess it's shorter). One advantage of that approach is that we could deprecate out='str' -> out='path' to give people time to change.

Activity

  1. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    I'm in favor of this change. With our usual deprecation cycle, I think it's OK if people need to update their code. I don't think you can expect to run an old analysis with the latest MNE-Python – for this use case, people can use older versions of MNE-Python. It's not worth adding a lot of custom code just to keep the old + behavior around.

  2. larsoner commented on Feb 10, 2022

    @larsoner
    MemberAuthor

    It's not worth adding a lot of custom code just to keep the old + behavior around.

    I agree it's not worth adding a lot, but it's going to be < 20 lines I think (for both classes!). Plus there is this:

    $ git grep ".*data_path.*+.*" | wc -l
    230
    

    Even internally we use it a lot, and I know from reading other people's scripts (and even some of my own when I get lazy) that it is pretty prevalent.

  3. hoechenberger commented on Feb 10, 2022

    @hoechenberger
    Member

    This has been on my wish list for years now, yes. The function says something about a path so it should return a Path :)

  4. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    Even internally we use it a lot, and I know from reading other people's scripts (and even some of my own when I get lazy) that it is pretty prevalent.

    We will update our code, and people can change their scripts. I'm talking about new scripts, I don't think we need to stay backwards compatible just so people can run their 5 year old scripts with the latest MNE version.

  5. larsoner commented on Feb 10, 2022

    @larsoner
    MemberAuthor

    I'm talking about new scripts, I don't think we need to stay backwards compatible just so people can run their 5 year old scripts with the latest MNE version.

    I think you're understating the problem a bit here -- people currently / today write code with this + pattern, so we're not talking about just (or even primarily) old stuff. If there is an easy workaround with subclassing that doesn't force people into the new pattern, I think we should implement it. There is always some risk with subclassing, but to me it's worth it for the usability gains for people. A common complaint from people is that MNE breaks things too often, and I'd like to avoid it more often than we have in the past.

  6. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    people currently / today write code with this + pattern, so we're not talking about just (or even primarily) old stuff.

    Yes, but they can adapt in their new scripts once we issue a deprecation warning.

    A common complaint from people is that MNE breaks things too often, and I'd like to avoid it more often than we have in the past.

    True, but many people are already using pathlib, so they know combining paths works with /. If we switch to returning pathlib.Path, I would find it strange that suddenly you can also concatenate with + only in MNE.

  7. hoechenberger commented on Feb 10, 2022

    @hoechenberger
    Member

    We can probably get around this, though, with some class magic -- we could subclass PosixPath and WindowsPath with support for a + operator, where if the other arg is a str then we cast to str and return op.join(path_str, other_str) (after stripping op.sep from the other_str).

    I don't like this idea, let's not introduce a new class here. This will just add loads of confusion.

    Let's add a param to data_path() that allows for returning Path or str, as suggested by @larsoner. It should default to returning a string for the next few releases, and issue a DeprecationWarning that this default will change (while the old behavior can be retained by using the param)

    btw I just also realized that many of our data_path() functions are missing docstrings, we should address that too.

  8. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    This sounds good, but I'm not sure if adding a parameter makes things simpler. A pathlib.Path is supposed to be a drop-in replacement for a str representing a path. If people would like to retain the old behavior (after a few versions of warnings) they can do str() on the output.

  9. larsoner commented on Feb 10, 2022

    @larsoner
    MemberAuthor

    This sounds good, but I'm not sure if adding a parameter makes things simpler.

    I don't think it makes things simpler, either, but it does allow us to move from pure str to pure Path via deprecation. Without a out-type of parameter, we can't properly deprecate. And if we don't deprecate, I think we're going to break a lot of stuff for a lot of people. Maybe it's okay since it's a 1.0 release, but still seems a bit painful...

    I don't like this idea, let's not introduce a new class here. This will just add loads of confusion.

    What's the loads of confusion / can you describe what a novice or advanced user will experience that will be problematic? For advanced users, we can mention in the docstring that we return an enhanced version of Path that supports + with str for backward compat. For novice users, they probably won't notice that + continues to work in scripts they already have, and can transition to the correct Path syntax of / whenever they get around to it.

  10. hoechenberger commented on Feb 10, 2022

    @hoechenberger
    Member

    This sounds good, but I'm not sure if adding a parameter makes things simpler. A pathlib.Path is supposed to be a drop-in replacement for a str representing a path. If people would like to retain the old behavior (after a few versions of warnings) they can do str() on the output.

    Fine with me too, however the idea with the new parameter stems from the assumption that we would like to retain the current behavior (returning a string) for a few versions while already allowing users to also get a Path instead.

    I would also be okay with removing this parameter again once we switch the default, but urgh, that's just another deprcation 😅😅🙈

  11. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    Yeah, proper deprecation requires having a new parameter. It won't hurt us keeping it around in the future, and if we really want to get rid of it we can do another round of deprecation as @hoechenberger suggested.

  12. hoechenberger commented on Feb 10, 2022

    @hoechenberger
    Member

    @larsoner

    This sounds good, but I'm not sure if adding a parameter makes things simpler.

    I don't think it makes things simpler, either, but it does allow us to move from pure str to pure Path via deprecation. Without a out-type of parameter, we can't properly deprecate.

    But what's the argument against introducing such a parameter? (Maybe I missed it in the discussion? 🤔)

    I don't like this idea, let's not introduce a new class here. This will just add loads of confusion.

    What's the loads of confusion / can you describe what a novice or advanced user will experience that will be problematic?

    I don't like the idea of introducing yet another way to handle paths. We (MNE users) already have strings and os.path; we have MNE-BIDS's BIDSPath with the .fpath attribute, which returns a pathlib.Path; and now we're talking about not only supporting pathlib.Path, but a subclass that deviates from the original. It just feels very messy to me. This would allow for 3 ways to concat paths:

    os.path.join(a, b)
    a / b
    a + b

    IDK, I'm not convinced…

  13. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    But what's the argument against introducing such a parameter? (Maybe I missed it in the discussion? 🤔)

    Just that it introduces something that isn't really necessary. It increases complexity, but it is required for deprecation, so let's do it.

  14. larsoner commented on Feb 10, 2022

    @larsoner
    MemberAuthor

    It just feels very messy to me. This would allow for 3 ways to concat paths:

    At the dev end, maybe -- but again to the end user, I think the experience is pretty straightforward. They should treat it like a Path, but for backward compat it happens to support this other operator. I don't expect users to struggle with this experienc (or to struggle myself to explain it to them).

    It increases complexity, but it is required for deprecation, so let's do it.

    I realized that there is a way that we can avoid a new param actually. I still don't like it as much because we force people to change a bunch of stuff, but we could:

    1. Return my magic subclass of Path that supports +, but also
    2. Have the + operation emit a DeprecationWarning that + support will go away in 1.1, so users should use / instead

    Then the magic is actually made explicit, people have some time to change their code (but they must do it by the next release), and in 1.1 we just return a plain Path (no more fancy magic subclass).

  15. cbrnr commented on Feb 10, 2022

    @cbrnr
    Contributor

    If the black magic eventually goes away this approach is pretty good!

  16. agramfort commented on Feb 10, 2022

    @agramfort
    SponsorMember

    I also consider that a depreciation cycle is necessary

  17. hoechenberger commented on Feb 10, 2022

    @hoechenberger
    Member

    @larsoner

    but we could:

    1. Return my magic subclass of Path that supports +, but also
    2. Have the + operation emit a DeprecationWarning that + support will go away in 1.1, so users should use / instead

    Sounds good to me!

  18. jasmainak commented on Feb 10, 2022

    @jasmainak
    Member

    I would like to vote in favor of a proper deprecation cycle. The threshold for breaking changes in MNE-ecosystem is a bit too low at the moment (and I'm myself to blame for this on many occasions). Even if you talk about one release cycle for deprecation, that's around 6 months. It's not too uncommon to come back to a script after 6 months or even a year. 5 years is maybe pushing it, but I'd love to see a script that ran 2 years ago to run today without any errors :)

  19. cbrnr commented on Feb 11, 2022

    @cbrnr
    Contributor

    I see your point, but why is it not possible for users and us to use an older version to run older code? This will always work no matter how many breaking changes we introduce. I agree we should not do that in every new release, but keeping ugly workarounds just so that the latest version runs old scripts is not worth the extra maintenance effort IMO.

  20. jasmainak commented on Feb 11, 2022

    @jasmainak
    Member

    But how do I build upon old code if I do not use the latest API? Maybe I don't need the latest function from MNE but I need the latest function from numpy to do this additional analysis but that version of numpy is not compatible with the old version of MNE. Of course, I could export certain files, import it in the new environment and then run that additional analysis but is that optimal from a user-perspective?

    what might be considered ugly from a coding point of view is actually beautiful from a UX point of view: the lack of unnecessary errors and warnings. The questions is: how does it affect the user experience besides looking ugly?

  21. cbrnr commented on Feb 11, 2022

    @cbrnr
    Contributor

    You build upon old code by replacing deprecated with supported code.

    Again, you can always run old scripts that worked with a given MNE version by using exactly that version. If you work on a new script (which includes taking an old script as a basis) then you will have to update certain things.

  22. cbrnr commented on Feb 11, 2022

    @cbrnr
    Contributor

    Also, remember that we are talking about a major version bump here – this is exactly meant for breaking API, so we could change it without deprecation. Semantic versioning implies that version 1.x.x is in general not compatible with 0.x.x.

  23. jasmainak commented on Feb 11, 2022

    @jasmainak
    Member

    In the past several months, whenever I run an MNE-related script ... I have had to spend first 15 minutes of my coding time fixing API related changes. Anyhow, I am just trying to add user perspective which is usually missing from the github discussions :)

  24. cbrnr commented on Feb 11, 2022

    @cbrnr
    Contributor

    Yes sure, but why don't you use the MNE version you used when you created the script? Ideally, you should already have an environment which preserves exact versions. But I know, that's probably not what the average user does.

    Anyhow, this discussion is drifting off a bit, so to come back to the original issue and proposed solution, I think we all agree that @larsoner's suggestion both uses proper deprecation and in two versions will be gone without any leftovers for us to keep maintaining - so a perfect solution to me!

  25. added this to the 1.0 milestone on Feb 11, 2022
  26. mmagnuski commented on Feb 11, 2022

    @mmagnuski
    Member

    I'm also +1 one for the subclassing witchcraft that raises deprecation warning and goes away in some time in future.

  27. hoechenberger commented on Feb 16, 2022

    @hoechenberger
    Member

    Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions