thombashi / pathvalidate

A Python library to sanitize/validate a string such as filenames/file-paths/etc.
https://pathvalidate.rtfd.io/
MIT License
220 stars 13 forks source link

Correct library interface #23

Closed chr7 closed 2 years ago

chr7 commented 2 years ago

pathvalidate 2.5.0

In Visual Studio Code, no suggestions are given for functions of pathvalidate, e.g. for sanitize_filename:

image

I thought that this is a problem of VSC's pylance therefore I opened this issue. It was closed with the comment that this is a problem how pathvalidate does the import. To fix it, it should be done as described in PEP561 resp. library interface.

thombashi commented 2 years ago

@chr7 Thank you for your report.

Could you tell me which part of the library violated PEP561? I thought pathvalidate follows PEP561 which includes py.typed file within the package and imported sanitize_filename at the top-level module.

Furthermore, code suggestions work correctly if I disable Pylance: image

chr7 commented 2 years ago

I can't tell you this, I forwarded this info from the pylance developer (see my issue there).

Anyhow, I switched the language server off (was set on pylance previously):

image

and I don't get any suggestions neither which isn't a surprise as the language server is off :-)

image

Then I tried Jedi:

image

I'll pass this info back to pylance, let's see what they say.

chr7 commented 2 years ago

According to the pylance developer 'Imported symbols are considered private by default. If they use the “import A as A”' https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md#library-interface

image

They suggest to fix that.

eggplants commented 2 years ago

When I used this library, mypy said:

getjump/getjump.py:111: error: Module has no attribute "sanitize_filepath"; maybe "sanitize_file_path",
"sanitize_filename", or "validate_filepath"?  [attr-defined]
            save_dir = pathvalidate.sanitize_filepath(
                       ^
getjump/main.py:91: error: Module has no attribute "sanitize_filepath"; maybe "sanitize_file_path",
"sanitize_filename", or "validate_filepath"?  [attr-defined]
            type=pathvalidate.sanitize_filepath,
                 ^
Found 2 errors in 2 files (checked 3 source files)

These errors are maybe related to this issue.


With __all__, no errors are reported.

diff --git a/pathvalidate/__init__.py b/pathvalidate/__init__.py
index 63f696c..3b55b9f 100644
--- a/pathvalidate/__init__.py
+++ b/pathvalidate/__init__.py
@@ -35,3 +35,43 @@ from .error import (
     ValidationError,
     ValidReservedNameError,
 )
+
+__all__ = (
+    "__author__",
+    "__copyright__",
+    "__email__",
+    "__license__",
+    "__version__",
+    "AbstractSanitizer",
+    "AbstractValidator",
+    "Platform",
+    "ascii_symbols",
+    "normalize_platform",
+    "replace_ansi_escape",
+    "replace_unprintable_char",
+    "unprintable_ascii_chars",
+    "validate_null_string",
+    "validate_pathtype",
+    "FileNameSanitizer",
+    "is_valid_filename",
+    "sanitize_filename",
+    "validate_filename",
+    "FilePathSanitizer",
+    "is_valid_filepath",
+    "sanitize_file_path",
+    "sanitize_filepath",
+    "validate_file_path",
+    "validate_filepath",
+    "sanitize_ltsv_label",
+    "validate_ltsv_label",
+    "replace_symbol",
+    "validate_symbol",
+    "ErrorReason",
+    "InvalidCharError",
+    "InvalidLengthError",
+    "InvalidReservedNameError",
+    "NullNameError",
+    "ReservedNameError",
+    "ValidationError",
+    "ValidReservedNameError",
+)
thombashi commented 2 years ago

@eggplants Thank you for your report. Could you tell me how to reproduce the mypy error? Python/pathvalidate/mypy version, sample code, etc.

eggplants commented 2 years ago
git clone https://github.com/eggplants/getjump
cd getjump
git checkout pathvalidate_error
pip install .
mypy getjump
getjump/getjump.py:113: error: Module has no attribute "sanitize_filepath"; maybe "sanitize_file_path",
"sanitize_filename", or "validate_filepath"?  [attr-defined]
                pathvalidate.sanitize_filepath(
                ^
getjump/main.py:60: error: Module has no attribute "is_valid_filepath"; maybe "validate_filepath",
"is_valid_filename", or "validate_file_path"?  [attr-defined]
        if pathvalidate.is_valid_filepath(v, platform="auto"):
           ^
Found 2 errors in 2 files (checked 3 source files)
eggplants commented 2 years ago

Or, just:

$ cat a.py
import pathvalidate

res = pathvalidate.sanitize_filepath("hello")
print(res)
$ pip install pathvalidate==2.5.0
...
$ mypy a.py --strict
a.py:3: error: Module has no attribute "sanitize_filepath"; maybe "sanitize_file_path", "sanitize_filename", or
"validate_filepath"?  [attr-defined]
    res = pathvalidate.sanitize_filepath("hello")
          ^
Found 1 error in 1 file (checked 1 source file)
$ pip uninstall pathvalidate
...
$ pip install git+https://github.com/eggplants/pathvalidate@add_all
...
$ mypy a.py --strict
Success: no issues found in 1 source file

$ mypy --version
mypy 0.960 (compiled: yes)
$ python --version
Python 3.10.4
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04 LTS
Release:        22.04
Codename:       jammy
thombashi commented 2 years ago

@eggplants Thank you for your information. I can reproduce the issue. Merged the PR and I had released pathvalidate 2.5.1.