1- # mypy reports many problems.
2- # type: ignore
31"""Rope preferences."""
2+ from enum import Enum
43from dataclasses import asdict , dataclass
54from textwrap import dedent
65from typing import Any , Callable , Dict , List , Optional , Tuple
@@ -36,6 +35,20 @@ class AutoimportPrefs:
3635 )
3736
3837
38+ @dataclass
39+ class ImportPrefs :
40+ preferred_import_style : str = field (
41+ default = "default" ,
42+ description = dedent ("""
43+ Controls how rope inserts new import statements. If set to
44+ ``"normal-import"`` (default) will insert ``import <package>``; if
45+ set to ``"from-module"`` will insert ``from <package> import
46+ <module>``; if set to ``"from-global"`` rope will insert ``from
47+ <package>.<module> import <object>``.
48+ """ ),
49+ )
50+
51+
3952@dataclass
4053class Prefs :
4154 """Class to store rope preferences."""
@@ -151,7 +164,7 @@ class Prefs:
151164 default = False ,
152165 description = dedent ("""
153166 If ``True`` modules with syntax errors are considered to be empty.
154- The default value is ``False``; When ``False`` syntax errors raise
167+ The default value is ``False``; when ``False`` syntax errors raise
155168 ``rope.base.exceptions.ModuleSyntaxError`` exception.
156169 """ ),
157170 )
@@ -166,8 +179,8 @@ class Prefs:
166179 prefer_module_from_imports : bool = field (
167180 default = False ,
168181 description = dedent ("""
169- If ``True``, rope will insert new module imports as ``from
170- <package> import <module>`` by default .
182+ **Deprecated**. ``imports.preferred_import_style`` takes
183+ precedence over ``prefer_module_from_imports`` .
171184 """ ),
172185 )
173186
@@ -234,7 +247,13 @@ class Prefs:
234247 """ ),
235248 )
236249 autoimport : AutoimportPrefs = field (
237- default_factory = AutoimportPrefs , description = "Preferences for Autoimport" )
250+ default_factory = AutoimportPrefs ,
251+ description = "Preferences for Autoimport" ,
252+ )
253+ imports : ImportPrefs = field (
254+ default_factory = ImportPrefs ,
255+ description = "Preferences for Import Organiser" ,
256+ )
238257
239258 def set (self , key : str , value : Any ):
240259 """Set the value of `key` preference to `value`."""
@@ -322,3 +341,21 @@ def get_config(root: Folder, ropefolder: Folder) -> PyToolConfig:
322341 global_config = True ,
323342 )
324343 return config
344+
345+
346+ class ImportStyle (Enum ): # FIXME: Use StrEnum once we're on minimum Python 3.11
347+ normal_import = "normal-import"
348+ from_module = "from-module"
349+ from_global = "from-global"
350+
351+
352+ DEFAULT_IMPORT_STYLE = ImportStyle .normal_import
353+
354+
355+ def get_preferred_import_style (prefs : Prefs ) -> ImportStyle :
356+ try :
357+ return ImportStyle (prefs .imports .preferred_import_style )
358+ except ValueError :
359+ if prefs .imports .preferred_import_style == "default" and prefs .prefer_module_from_imports :
360+ return ImportStyle .from_module
361+ return DEFAULT_IMPORT_STYLE
0 commit comments