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

✨ Add ignore patterns #92

Merged
merged 16 commits into from
Jul 18, 2023
Merged

✨ Add ignore patterns #92

merged 16 commits into from
Jul 18, 2023

Conversation

lemonyte
Copy link
Contributor

@lemonyte lemonyte commented Jul 13, 2023

Allows the user to specify glob patterns to ignore when scanning for source files.

Example usage:

# Ignore everything in the scripts directory.
bump-pydantic --ignore=scripts/** .
# Ignore only files in the tests directory, but don't ignore more deeply nested files.
bump-pydantic --ignore=tests/* .
# Ignore only files under directories named foo.
bump-pydantic --ignore=**/foo/* .
# Ignore all __init__.py files.
bump-pydantic --ignore=**/__init__.py .

Note: on POSIX systems it may be necessary to escape patterns in the command line.

bump-pydantic --ignore=scripts/\*\* .

Implementation details and design decisions

  • This PR adds a function glob_to_re() which translates a glob pattern into a regular expression. The function is necessary because:

    • the glob module does not provide a way to match a path against a pattern without traversing the file system, and
    • the fnmatch.fnmatch() and pathlib.Path.match() functions do not handle recursive wildcards (**).
  • This feature requires disabling automatic argument expansion on Windows in Click. If not disabled, option strings like .venv/** will be transformed into .venv\\ before bump-pydantic is able to apply its own interpretation of the string. The only way to pass this argument to Click is during the invocation of the app instance, therefore it was necessary to make the executable function a functools.partial.

  • Extra: a list of default ignore patterns has been added, currently only containing .venv/**.

  • Extra: if no files are left either because there are no source files in the given directory, or as a result of filtering with ignore patterns, the program now exits gracefully instead of having Mypy raise an error.

Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

Can we add some unit tests for glob_to_re?

@@ -26,9 +28,15 @@
add_completion=False,
)

entrypoint = functools.partial(app, windows_expand_args=False)
Copy link
Member

Choose a reason for hiding this comment

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

Why can't this be added to the app instead of using this partial?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Neither Typer nor Click seem to have an option to pass this argument during initialization. The only way to pass it is during invocation of the app through the __call__() or main() methods.

Copy link
Member

Choose a reason for hiding this comment

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

Do you have a reference for this?

Copy link
Contributor Author

@lemonyte lemonyte Jul 17, 2023

Choose a reason for hiding this comment

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

PR pallets/click#1918 added the parameter, only to the main method.
The only mention of this parameter in Click's documentation is under the main method here.
pallets/click#1918 (comment) explains how to use it in an entrypoint.

bump_pydantic/main.py Outdated Show resolved Hide resolved
@lemonyte lemonyte requested a review from Kludex July 15, 2023 22:28
bump_pydantic/helpers.py Outdated Show resolved Hide resolved
@lemonyte lemonyte requested a review from Kludex July 17, 2023 20:40
Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

Thanks @lemonyte :)

@Kludex Kludex merged commit 393dc89 into pydantic:main Jul 18, 2023
@lemonyte lemonyte deleted the ignore-patterns branch July 25, 2023 09:30
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.

Feature request: ignore patterns for files or directories
2 participants