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

Correct library interface #23

Closed
chr7 opened this issue Apr 5, 2022 · 8 comments
Closed

Correct library interface #23

chr7 opened this issue Apr 5, 2022 · 8 comments

Comments

@chr7
Copy link

chr7 commented Apr 5, 2022

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
Copy link
Owner

thombashi commented Apr 8, 2022

@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
Copy link
Author

chr7 commented Apr 12, 2022

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
Copy link
Author

chr7 commented Apr 14, 2022

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
Copy link
Contributor

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",
+)

@eggplants eggplants mentioned this issue Jul 23, 2022
@thombashi
Copy link
Owner

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

@eggplants
Copy link
Contributor

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
Copy link
Contributor

eggplants commented Jul 24, 2022

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
Copy link
Owner

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

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

No branches or pull requests

3 participants