[
https://issues.apache.org/jira/browse/THRIFT-1697?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15120386#comment-15120386
]
Estevan Aguayo edited comment on THRIFT-1697 at 1/27/16 11:21 PM:
------------------------------------------------------------------
Still and issue in thrift 9.3
This is very problematic if your thrift files are not in the root of your
project and you are using an include:
For example:
idl1.thrift
idl2.thrift (include "idl1.thrift")
This results in the following packages being created:
idl1/__init__.py
idl1/ttpyes.py
idl2/__init__.py
idl2/ttypes.py
in the file idl2/ttypes.py the import happens using relative notion and looks
like so:
from thrift.Thrift import TType, TMessageType, TException, TApplicationException
import idl1.ttypes
This works all well in good if your project is set up like so:
proj/
proj/main.py
proj/idl1/
proj/idl2/
Because python finds the idl1 package at the root of the project it works fine.
If you restructure like so however:
proj/
proj/main.py
proj/subpackage/
proj/subpackage/idl1/
proj/subpackage/idl2/
The import fails because it cannot find idl1 packge. To correct this the import
in idl2 should be changed to
from ..idl1 import ttypes
This becomes a major pain point you need to version your classes (in a package).
The idea that all thrift generated code needs to live in the same package (or
at the root of the project structure) is fine however anything that extends a
base IDL using "include" will run into this issue.
was (Author: eaguayo):
This is very problematic if your thrift files are not in the root of your
project and you are using an include:
For example:
idl1.thrift
idl2.thrift (include "idl1.thrift")
This results in the following packages being created:
idl1/__init__.py
idl1/ttpyes.py
idl2/__init__.py
idl2/ttypes.py
in the file idl2/ttypes.py the import happens using relative notion and looks
like so:
from thrift.Thrift import TType, TMessageType, TException, TApplicationException
import idl1.ttypes
This works all well in good if your project is set up like so:
proj/
proj/main.py
proj/idl1/
proj/idl2/
Because python finds the idl1 package at the root of the project it works fine.
If you restructure like so however:
proj/
proj/main.py
proj/subpackage/
proj/subpackage/idl1/
proj/subpackage/idl2/
The import fails because it cannot find idl1 packge. To correct this the import
in idl2 should be changed to
from ..idl1 import ttypes
This becomes a major pain point you need to version your classes (in a package).
The idea that all thrift generated code needs to live in the same package (or
at the root of the project structure) is fine however anything that extends a
base IDL using "include" will run into this issue.
> Generate *explicit* relative imports in Python, rather than implicit ones
> -------------------------------------------------------------------------
>
> Key: THRIFT-1697
> URL: https://issues.apache.org/jira/browse/THRIFT-1697
> Project: Thrift
> Issue Type: Improvement
> Components: Python - Compiler
> Reporter: Alex Gaynor
> Attachments: thrift-1697-python-relative-imports.2.patch,
> thrift-1697-python-relative-imports.patch
>
>
> Currently the compiler generates code that looks like:
> import ttypes
> Which relies on the implicit import machinery to load the module in the same
> directory.
> It would be much better to use
> from . import ttypes
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)