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

Path.unlink should have a missing_ok parameter #77304

Closed
rbu mannequin opened this issue Mar 22, 2018 · 4 comments
Closed

Path.unlink should have a missing_ok parameter #77304

rbu mannequin opened this issue Mar 22, 2018 · 4 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rbu
Copy link
Mannequin

rbu mannequin commented Mar 22, 2018

BPO 33123
Nosy @pitrou, @serhiy-storchaka, @lordmauve, @miss-islington, @rbu
PRs
  • bpo-33123: pathlib: Add missing_ok parameter to Path.unlink #6191
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-05-15.22:06:00.865>
    created_at = <Date 2018-03-22.17:56:36.197>
    labels = ['3.8', 'type-feature', 'library']
    title = 'Path.unlink should have a missing_ok parameter'
    updated_at = <Date 2019-05-15.22:06:00.864>
    user = 'https://github.com/rbu'

    bugs.python.org fields:

    activity = <Date 2019-05-15.22:06:00.864>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-15.22:06:00.865>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2018-03-22.17:56:36.197>
    creator = 'rbu'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33123
    keywords = ['patch']
    message_count = 4.0
    messages = ['314277', '321822', '321831', '342605']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'serhiy.storchaka', 'lordmauve', 'miss-islington', 'rbu']
    pr_nums = ['6191']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue33123'
    versions = ['Python 3.8']

    @rbu
    Copy link
    Mannequin Author

    rbu mannequin commented Mar 22, 2018

    Similarly to how several pathlib file creation functions have an "exists_ok" parameter, we should introduce "missing_ok" that makes removal functions not raise an exception when a file or directory is already absent.

    IMHO, this should cover Path.unlink and Path.rmdir.

    Note, Path.resolve() has a "strict" parameter since 3.6 that does the same thing. Naming this of this new parameter tries to be consistent with the "exists_ok" parameter as that is more explicit about what it does (as opposed to "strict").

    @rbu rbu mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Mar 22, 2018
    @serhiy-storchaka
    Copy link
    Member

    It can be written as

    try:
        path.inlink()
    except FileNotFoundError:
        pass
    

    If you want to save few lines of code, you can use contextlib.suppress().

        with suppress(FileNotFoundError): path.inlink()

    I suggest to close this issue. It is better to keep the API simple and orthogonal. Adding an option in Path.unlink() will require adding this support of this option in third-part implementations of Path. In general, adding a single boolean parameter is not considered a good practice in Python.

    A "strict" parameter in Path.resolve() does the different thing. In both cases Path.resolve() returns a value, and you can't implement strict=False by catching exception externally.

    @lordmauve
    Copy link
    Mannequin

    lordmauve mannequin commented Jul 17, 2018

    This would be a shortcut in the common case that you simply want an idempotent "make sure this file/symlink is gone" operation.

    There are already boolean options to enable idempotent behaviour in several pathlib implementations, such as mkdir(exist_ok=True) and touch(exist_ok=True). write_bytes() and write_text() are also idempotent. unlink() aligns well with this.

    Because this operation doesn't exist, developers are tempted to write

        if path.exists():
            path.unlink()

    which both has a TOCTTOU bug and doesn't correctly handle symlinks.

    @miss-islington
    Copy link
    Contributor

    New changeset d9e006b by Miss Islington (bot) (‮zlohhcuB treboR) in branch 'master':
    bpo-33123: pathlib: Add missing_ok parameter to Path.unlink (GH-6191)
    d9e006b

    @pitrou pitrou closed this as completed May 15, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants