Skip to content

Conversation

untzag
Copy link

@untzag untzag commented Aug 14, 2020

See also #120, #123

I'd really like pathlib support, so I thought I'd try my hand at an implementation to see if I can get something merged. This implementation builds off of the comments made in #123 by having completely separate functions / class so that current behavior will not change at all.

Currently my implementation falls back on returning strings if it fails to import pathlib. Is this desired behavior, or would you rather raise an exception if pathlib isn't available? We could also move pathlib to another file _pathlib.py and only import these functions / class into the appdirs namespace if pathlib is available.

@Akuli
Copy link

Akuli commented Aug 21, 2020

Why is AppPaths a subclass of AppDirs? Adding more properties to a class doesn't break backwards compatibility.

Edit: I was thinking of having e.g. user_cache_path and user_cache_dir, so that anything suffixed with _path returns pathlib.Path and everything else returns strings.

@untzag
Copy link
Author

untzag commented Aug 21, 2020

@Akuli hey that's a good idea! I originally had some thought about AppPaths being a "drop in" replacement for AppDirs, but now that you say it I think I like your idea more...

I'd love to hear some input from an appdirs maintainer, what approach do y'all prefer?

@Akuli
Copy link

Akuli commented Aug 21, 2020

Currently my implementation falls back on returning strings if it fails to import pathlib. Is this desired behavior...

Can you give an example of a use case where this behaviour would be useful? I can't think of any. I either want Paths or I want strings, and I never want something that might return either one, hiding the ImportError that caused it to return strings.

I think the simplest way to do this might be:

class AppDirs:
    ...
    @property
    def site_data_path(self):
        import pathlib
        return pathlib.Path(self.site_data_dir)

Things that I consider advantages with this approach:

  • No need to check Python versions
  • Import errors are never hidden
  • Never returns strings
  • It's very simple

Possible disadvantage:

  • Property access raising ImportError can be confusing (but fortunately not many people are writing new Python 2 code)

I'd like to know what other people think.

Also, if this was my project, I would want _pathlib_wrapper to be replaced with code like this, just to make sure that anybody understands the code:

def site_data_path(*args, **kwargs):
    import pathlib
    return pathlib.Path(site_data_dir(*args, **kwargs))

def site_config_path(*args, **kwargs):
    import pathlib
    return pathlib.Path(site_config_dir(*args, **kwargs))

...

@untzag untzag closed this Aug 25, 2022
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.

4 participants