Code Monkey home page Code Monkey logo

absolufy-imports's Introduction

Hello everybody,

I work at Quansight Labs and spend time on:

  • Polars
  • pandas
  • assorted consluting / training

I've also written some assorted little tools:

  • cython-lint (the world's first Cython linter?)
  • absolufy-imports (superseded by ruff)
  • nbQA (mostly superseded by ruff, but still gets ~100k downloads/month for some reason)
  • auto-walrus (hopefully won't be implemented in ruff)
  • polars-upgrade (the only way is up)
  • polars-xdt (eXtra stuff for Dates 'n' Times)
  • narwhals (🎵singing in the ocean, causing a commotion🎶)

and a tutorial on how to write Polars Plugins: https://marcogorelli.github.io/polars-plugins-tutorial/. Don't be scared

absolufy-imports's People

Contributors

bskinn avatar marcogorelli avatar pre-commit-ci[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

absolufy-imports's Issues

Packages and Python built-in imports are not supported when the project has somewhere a folder with the same name

Bugs:

# Original (correct) import
from collections.abc import Iterable

# Changed automatically to a wrong
from ..abc import Iterable
# Original (correct) import
from google.cloud import vision

# Changed automatically to a wrong
from .cloud import vision

Project structure:

project/
    src/
        collections/
            foo/
            bar/
        google/
            spam/
            eggs/

As you see, if a sub-directory matches 3rd-party package or Python module, the 'absolufy-imports' makes mistakes.

Possible solutions

  • Check that import is a 3rd-party package. Do not make such imports relative.
  • Another possible solution is to allow to ignore manually specific names. For example, I will ignore names "collections" and "google".

Can't parse files with BOM

absolufy-imports 0.3.0 cannot parse a python file with a BOM.

For example, download bom.py.txt and rename it bom.py (this is a necessary step on GitHub as only *.txt files can be attached), move it into an example project (e.g. src), see the following:

$ cat src/bom.py
import sys
$ file src/bom.py	
src/bom.py: UTF-8 Unicode (with BOM) text
$ python src/bom.py
$ absolufy-imports src/bom.py
Traceback (most recent call last):
  File "/tmp/py310/bin/absolufy-imports", line 8, in <module>
    sys.exit(main())
  File "/tmp/py310/lib/python3.10/site-packages/absolufy_imports.py", line 208, in main
    absolute_imports(
  File "/tmp/py310/lib/python3.10/site-packages/absolufy_imports.py", line 151, in absolute_imports
    tree = ast.parse(txt)
  File "/opt/python-3.10.0/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    import sys
    ^
SyntaxError: invalid non-printable character U+FEFF

Perhaps a better message could help? Or try to compare these two uses reading the source file, the last with encoding='utf-8-sig':

$ python -c "from ast import parse; from pathlib import Path; parse(Path('src/bom.py').read_text())"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/python-3.10.0/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    import sys
    ^
SyntaxError: invalid non-printable character U+FEFF
$ python -c "from ast import parse; from pathlib import Path; parse(Path('src/bom.py').read_text(encoding='utf-8-sig'))"
$ 

Define an authorized level of relativity

I would love to use your module but it is lacking something essential IMO: allow relative imports inside the same folder, i.e, allow from .a import b but not allow from ..a import b.

Most people find it more legible to reference siblings files relatively as you can see/list them. However, as soon as the import to parent folders, it becomes very difficult to read, specially from ............a import b

maybe we could add a relative_level: int to the settings, with

  • 0 meaning replace all relative imports
  • 1 meaning keep only single dot (ex from .a import b)
  • 2 meaning keep up to two dots (ex from ..a import b)
  • etc...

FR: support usage without pre-commit

Great project! We don't use pre-commit but would like to use this in our CI and locally.

This would require:

  1. Similar to black --check and isort --check-only, a way to only check for compliance via the exit code without modifying anything.
  2. The ability to pass directories rather than individual files.
  3. (optional) Maybe add --diff too.

--keep-relative-imports in __init__.py files only

Hello Marco,

Thank you for great tool. It very helps me to keep source clean.

I have a question and\or suggestion. Is it possible to add command-line argument to make keep-submodules-relative works on init.py files only?

It is very common to use init.py file as a facade to classes and functions from the module, and abosolute imports are quiet annoying here:

# my_application/package/another_package/__init__.py
from .foo import bar
from .foo1 import bar1

vs

# my_application/package/another_package/__init__.py
from my_application.package.another_package.foo import bar
from my_application.package.another_package.foo1 import bar1

It doesn't work on the cuda-python repo

NVIDIA/cuda-python#38

I did

(cython) nyck33@nyck33-IdeaPad-Gaming-3-15ACH6:~/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction$ python clock_nvrtc_test.py
Traceback (most recent call last):
  File "/home/nyck33/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction/clock_nvrtc_test.py", line 10, in <module>
    from examples.common import common
ModuleNotFoundError: No module named 'examples'
(cython) nyck33@nyck33-IdeaPad-Gaming-3-15ACH6:~/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction$ ls
clock_nvrtc_test.py  simpleCubemapTexture_test.py  simpleZeroCopy_test.py     vectorAddDrv_test.py
__init__.py          simpleP2P_test.py             systemWideAtomics_test.py  vectorAddMMAP_test.py
(cython) nyck33@nyck33-IdeaPad-Gaming-3-15ACH6:~/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction$ absolufy-imports clock_nvrtc_test.py
(cython) nyck33@nyck33-IdeaPad-Gaming-3-15ACH6:~/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction$ absolufy-imports clock_nvrtc_test.py
(cython) nyck33@nyck33-IdeaPad-Gaming-3-15ACH6:~/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction$ python clock_nvrtc_test.py
Traceback (most recent call last):
  File "/home/nyck33/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction/clock_nvrtc_test.py", line 10, in <module>
    from examples.common import common
ModuleNotFoundError: No module named 'examples'
(cython) nyck33@nyck33-IdeaPad-Gaming-3-15ACH6:~/Documents/cuda-start-dec2022/cuda-python/examples/0_Introduction$ 

Provide directory or package as input

Hello!

Awesome project - I was very happy when found it, and this is a really time saver :)

I have a Django project with a following structure:

server
├── Dockerfile
├── action_requests (package)
├── config (package)
├── manage.py
├── persons (package)

While using this amazing tool I faced with a problem when I tried to pass a whole directory of project to absolufy-imports and this caused problems:

▶ absolufy-imports server
Traceback (most recent call last):
  File "/Users/artinnok/Library/Caches/pypoetry/virtualenvs/backend-6XZmvTyF-py3.9/bin/absolufy-imports", line 8, in <module>
    sys.exit(main())
  File "/Users/artinnok/Library/Caches/pypoetry/virtualenvs/backend-6XZmvTyF-py3.9/lib/python3.9/site-packages/absolufy_imports.py", line 208, in main
    absolute_imports(
  File "/Users/artinnok/Library/Caches/pypoetry/virtualenvs/backend-6XZmvTyF-py3.9/lib/python3.9/site-packages/absolufy_imports.py", line 149, in absolute_imports
    with open(file, encoding='utf-8') as fd:
IsADirectoryError: [Errno 21] Is a directory: 'server'

Then I tried to do the same with package action_requests inside server project and faced with same issue.

In every package I have about 15-20 Python files and it is tedious to run absolufy-imports on every file of package :(((
Is it possible to add support for folders / packages in future?

Thanks for great tool.

P.S. I think this will force me to write something like a bash-wrapper around absolufy-imports :((

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.