Suffix in pathlib is not behaving like a file extension
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Bug report
Bug description:
According to the docs here a suffix is defined as:
The file extension of the final component, if any
But pathlib doesn't behave as expected, illustrating that on Python 3.12.4:
>>> Path("Mr. Smith resume for review").suffix
'. Smith resume for review'
This is particularly problematic for methods like with_suffix. According to the docs here that should:
Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead. If the suffix is an empty string, the original suffix is removed
But as established above, that results in:
>>> Path("Mr. Smith resume for review").with_suffix(".pdf")
PosixPath('Mr.pdf')
I've characterized that as a bug as that is not working as described, but could also be a matter of documentation improvement.
I see a few ways:
- Implement
add_suffix, or an argument towith_suffix, but I know that has been discussed before and was ultimately decided against. Also this is a matter of how suffix is defined, not how it's processed. - Sanity check what a suffix can be, e.g. suffix can't have white space. But ultimately difficult without making assumption.
- Adjust the documentation: Clarify that suffix is not equal to file extension but something like last segment separated by a dot or similar. Maybe even with a warning that this can have unexpected behavior as showcased above.
CPython versions tested on:
3.12
Operating systems tested on:
Linux, Windows
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire les exemples de Python 3.12 pour pathlib concernant PurePath.suffix et with_suffix, puis examinez l’implémentation et la documentation de pathlib pour déterminer comment les suffixes sont définis. Comparez le comportement observé avec la formulation documentée concernant les extensions de fichier et déterminez si le problème est résolu par des changements de comportement ou par une clarification de la documentation.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- operating-systems
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100