How to safely "fake" a module in a Python package

Currently, I have the following directory structure in the main branch git:

/dir1
    __init__.py
    module.py

This will be changed to (in my branch):

/dir1
    __init__.py
    /dir2
        module1.py # With 70% of code of module.py
        module2.py # With 30% of code of module.py

Questions:

  • I know that it is impossible to gittrack both new files, but since it gitrecognizes renames (and considers the organization in folders as renaming), I can track changes in module.pythe leading branch to my branch module1.py, at least for 70% of the code (I will have to update module2.pymanually ) So is there a better way to handle this?

  • API , , , from dir1.module import abc ( module.py dir1). , , sys, . API /?


. :

/dir1
    __init__.py
    module_2.py
        def add2(a, b):
            return a + b
        def sub2(a, b):
            return a - b
    module_3.py
        def add3(a, b, c):
            return a + b + c

/dir1
    __init__.py
    /dir2
        __init__.py
        module_add.py
            # ... Constitutes 70% of code from `dir1/module_2.py`
            def add2(a, b):
                return a + b
            # A few more additional lines added from `dir1/module_3.py`
            def add3(a, b, c):
                return a + b + c
        module_sub.py
            # Constitutes the 30% from /dir1/module2.py
            def sub2(a, b):
                return a - b

, , dir1/module_2.py dir1/module_3.py module_add.py module_sub.py /dir1/dir2

, 1, 2, :

from module_2 import add2, sub2
from module_3 import add3

, :

  • module_2.py module_3.py dir1 ( git dir1/module_2.py dir1/dir2/module_2.py );
  • sys.path , /;
  • dir2, . module_2.
+4
1

, :

/dir1
    __init__.py
        from module import abc
    module.py
        abc = None

( ) :

/dir1
    __init__.py
        from module import abc
    /module
        __init__.py
           from module1 import abc
        module1.py  # this is the moved and renamed module.py, with git history
            abc = None
        module2.py  # this is the 30% you've factored out
            # whatever in here

module.py/module from module import abc ( from dir1.module import abc ..) .


:

/dir1
    __init__.py
        from module_2 import add2, sub2
        from module_3 import add3
    module_2.py
    module_3.py

/dir1
    __init__.py
        from dir2.module_add import add2, add3
        from dir2.module_sub import sub2
    /dir2
        __init__.py
        module_add.py  # module_2.py moved & renamed
        module_sub.py  # module_3.py moved & renamed or new file
    /module_2
        __init__.py
           from ..dir2.module_add import add2
           from ..dir2.module_sub import sub2
    /module_3
        __init__.py
           from ..dir2.module_add import add3

(, from dir1.module_2 import add2) , (, from dir1.dir2.module_add import add2, add3).


, :

import warnings
warnings.warn("deprecated", DeprecationWarning)

__init__.py /dir1/module_2 /dir1/module_3, , . :

>>> import warnings
>>> warnings.simplefilter('always')
>>> from dir1.dir2.module_sub import sub2
>>> sub2(1, 2)
-1
>>> from dir1.module_3 import add3

Warning (from warnings module):
  File "dir1\module_3\__init__.py", line 2
    warnings.warn("deprecated", DeprecationWarning)
DeprecationWarning: deprecated
>>> add3(1, 2, 3)
6
+4

All Articles