Replies: 1 comment
-
| 
         To answer my own question,  Here is my implementation of this feature diff --git a/isort/parse.py b/isort/parse.py
index c60938d8..c9c12cd3 100644
--- a/isort/parse.py
+++ b/isort/parse.py
@@ -182,6 +182,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
     index = 0
     import_index = -1
     in_quote = ""
+    in_class = False
     while index < line_count:
         line = in_lines[index]
         index += 1
@@ -265,7 +266,14 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
         for statement in statements:
             line, raw_line = _normalize_line(statement)
-            type_of_import = import_type(line, config) or ""
+            if line.startswith('class '):
+                in_class = True
+            elif line.startswith('def '):
+                in_class = False
+            if in_class and (line.startswith('    import') or line.startswith('    from')):
+                type_of_import = import_type(line.lstrip(), config) or ""
+            else:
+                type_of_import = import_type(line, config) or ""
             raw_lines = [raw_line]
             if not type_of_import:
                 out_lines.append(raw_line)
@@ -371,7 +379,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
                     parts = import_string.split(" import ")
                     cimports = False
-                from_import = parts[0].split(" ")
+                from_import = parts[0].lstrip().split(" ")
                 import_string = (" cimport " if cimports else " import ").join(
                     [from_import[0] + " " + "".join(from_import[1:])] + parts[1:]
                 ) | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I like the convenience of writing my imports on top (before) of the current function and have isort float it to the top. However this only works with function and not when writing methods? Can imports inside a class also be floated to the top of the file?
Beta Was this translation helpful? Give feedback.
All reactions