Comments (5)
If you acknowledge that this needs to addressed by pathspec
, I think the place to fix this would be util.normalize_file()
.
When it creates the str
. It should check if it currently handles a Path
and if yes, append the trailing slash if Path.is_dir()
.
from python-pathspec.
@yschroeder I'm undecided on this at the moment. I'm leaning towards supporting it but it's kind of inconsistent.
Supporting Path.is_dir()
would be convenient but its behavior is not immediately apparent unless you're looking at the file-system. Path.is_dir()
is only true when the directory exists on the file-system. It's false otherwise, even if constructed as Path('important/')
. This makes sense when you think of it as an analog to os.path.isdir()
. PurePath.is_dir()
does not exist because it represents a virtual path that doesn't necessarily exist on the file-system. It would be nice if Path
and PurePath
maintained trailing slashes.
Does black always use Path
with real file-system paths as opposed to PurePath
or paths that don't exist?
from python-pathspec.
Yes, I am also not too sure, as Path.is_dir()
requires that the path actually exists in the file system. pathspec
only works on strings and doesn't care if the path exists or not, which is a good thing. I realized this problem when I tried to create a test case for pathspec
which would require a mock for is_dir()
as otherwise the test would need real files and folders.
Disclaimer: I am not a developer of black
but only a user, so I might be completely wrong here.
What black
does is:
- Find the root directory of the repository
- Create a
PathSpec
for the found.gitignore
- Iterate over all elements of the root directory via
Path.iter_dir()
- Check the paths from step 3 with
match_file()
of the spec from step 2 - If step 4 did not match, format that file or decend into the directory
- Repeat above steps in the subdirectory
In step 4, the match_file()
fails and we do not decend into the subdirectory in step 5 although we should.
TL;DR: black
uses Path
with real file-system paths which really exist (and tries to check directories with match_file()
).
from python-pathspec.
Thanks for the summary. After thinking about this, I don't think the right behavior is to treat Path
with .is_dir()
specially because its behavior is so tied to the file-system. What may be useful is I can add a utility function, say append_dir_sep()
, which appends/adds the trailing path separator when Path.is_dir()
is True
. It would essentially be:
def append_dir_sep(path: pathlib.Path) -> str:
out_path = str(path)
if path.is_dir():
out_path += os.sep
return out_path
Then to use it you can do:
from pathspec.util import append_dir_sep
relative_path = append_dir_sep(relative_path)
if pattern.match_file(relative_path):
pass
On a side note, looking the black's usage that you pointed out, here, it looks like relative_path
is already a str
when it's passed to pattern.match_file()
because normalize_path_maybe_ignore()
returns a str
. With this in mind, the normalize_path_maybe_ignore()
function would have to be modified to either use append_dir_sep()
or add the dir separator itself.
from python-pathspec.
I've added the pathspec.util.append_dir_sep()
function in the new release, v0.10.3. I believe that is all that I can address for this issue due to limitations with Path
and the implementation of black.
from python-pathspec.
Related Issues (20)
- Symlink pathspec_meta.py breaks Windows HOT 1
- test_util.py uses os.symlink which can fail on Windows HOT 1
- Backslashes at start of pattern not handled correctly HOT 1
- `!` doesn't exclude files in directories if the pattern doesn't have a trailing slash HOT 1
- Dist failure for Fedora, CentOS, EPEL HOT 11
- Since version 0.10.0 pure wildcard does not work in some cases HOT 4
- The pattern_to_regex method does not seem to work correctly on windows. HOT 4
- IndexError with my .gitignore file when trying to build a Python package HOT 4
- Package not marked as `py.typed` HOT 1
- Exports are considered private HOT 1
- `'Self'` string literal type is `Unknown` in pyright HOT 1
- Please consider switching the build-system to flit_core to ease setuptools bootstrap HOT 5
- Include directory should override exclude file HOT 3
- On bracket expression negation HOT 2
- match_files with negated path spec HOT 5
- `GitIgnoreSpec` behaviors differ from git HOT 2
- PathSpec.match_file() returns None since 0.12.0 HOT 3
- Exclusions not working HOT 2
- Leading & trailing whitespace HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-pathspec.