Repository: timothycrosley/isort (python)

Issue:
force_to_top not respected by force_sort_within_sections
force_to_top seems to be applied before sections are sorted when force_sort_within_sections is specified:

testisort.py

```
import foo
import z
from foo import bar
```

~/.isort.cfg

```
[settings]
force_to_top=z
force_sort_within_sections=True
```

```
$ isort testisort.py
```

testisort.py

```
import foo
from foo import bar
import z
```

These seems like extremely counter-intuitive behavior. 


Pull request description:
Feature/fix issue 473
Fixes #473 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6d09727e..cbf5ef28 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ IMPORTANT NOTE: This will be the last release with Python 2.6 support, subsequen
 - Fixed issue #491: Non ending parentheses withing single line comments no longer cause formatting issues
 - Fixed issue #471: Imports that wrap the maximum line length and contain comments on the last line are no longer rendered incorrectly
 - Fixed issue #436: Force sort within section no longer rearranges comments
+- Fixed issue #473: Force_to_top and force_sort_within_sections now work together
 - Fixed issue #484 & #472: Consistent output with imports of same spelling but different case
 Breaking Changes:
     - Fixed issue #511: All command line options with an underscore, have had the underscore replaced with a dash for consistency. This effects: multi-line, add-import, remove-import, force-adds, --force-single-line-imports, and length-sort.
diff --git a/isort/isort.py b/isort/isort.py
index 7f8cc97d..d07b4f17 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -536,14 +536,17 @@ class SortImports(object):
 
             if self.config['force_sort_within_sections']:
                 def by_module(line):
+                    section = 'B'
                     if line.startswith('#'):
-                        return '#'
+                        return 'AA'
 
                     line = re.sub('^from ', '', line)
                     line = re.sub('^import ', '', line)
+                    if line.split(' ')[0] in self.config['force_to_top']:
+                        section = 'A'
                     if not self.config['order_by_type']:
                         line = line.lower()
-                    return line
+                    return '{0}{1}'.format(section, line)
                 section_output = nsorted(section_output, key=by_module)
 
             if section_output:


Test patch:
diff --git a/test_isort.py b/test_isort.py
index bb7fd84d..5b959bed 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2084,6 +2084,7 @@ def test_inconsistent_behavior_in_python_2_and_3_issue_479():
                   'from workalendar.europe import UnitedKingdom\n')
     assert SortImports(file_contents=test_input).output == test_input
 
+
 def test_sort_within_section_comments_issue_436():
     """Test to ensure sort within sections leaves comments untouched"""
     test_input = ('import os.path\n'
@@ -2094,3 +2095,12 @@ def test_sort_within_section_comments_issue_436():
                   '# it must not be ...      comment line 3\n'
                   'import report\n')
     assert SortImports(file_contents=test_input, force_sort_within_sections=True).output == test_input
+
+
+def test_sort_within_sections_with_force_to_top_issue_473():
+    """Test to ensure it's possible to sort within sections with items forced to top"""
+    test_input = ('import z\n'
+                  'import foo\n'
+                  'from foo import bar\n')
+    assert SortImports(file_contents=test_input, force_sort_within_sections=True,
+                       force_to_top=['z']).output == test_input