-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Black pyi files #308
Draft
Zeckie
wants to merge
2
commits into
fabioz:master
Choose a base branch
from
Zeckie:black-pyi-files
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Black pyi files #308
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
plugins/org.python.pydev/tests/org/python/pydev/editor/actions/PyFormatBlackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.python.pydev.editor.actions; | ||
|
||
import org.eclipse.jface.text.Document; | ||
import org.python.pydev.ast.formatter.PyFormatter; | ||
import org.python.pydev.core.docutils.SyntaxErrorException; | ||
import org.python.pydev.core.formatter.FormatStd; | ||
import org.python.pydev.core.formatter.FormatStd.FormatterEnum; | ||
import org.python.pydev.shared_core.resource_stubs.AbstractIFileStub; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class PyFormatBlackTest extends TestCase { | ||
private final class MockIFile extends AbstractIFileStub { | ||
private final String ext; | ||
|
||
private MockIFile(String ext) { | ||
this.ext = ext; | ||
} | ||
|
||
@Override | ||
public String getFileExtension() { | ||
return ext; | ||
} | ||
} | ||
|
||
static final String SRC = "from typing import (\n" | ||
+ " Union, Dict\n" | ||
+ " )\n" | ||
+ "def a()->None: \n" | ||
+ " ...\n" | ||
+ "def b(_:Union[Dict[str,str],str])->int: \n" | ||
+ " ...\n" | ||
+ "d = [1, 2, 3]"; | ||
|
||
private static boolean DEBUG = false; | ||
|
||
public static void main(String[] args) { | ||
try { | ||
PyFormatBlackTest n = new PyFormatBlackTest(); | ||
junit.textui.TestRunner.run(PyFormatBlackTest.class); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
private FormatStd black; | ||
|
||
private void checkFormat(String ext, String expected) { | ||
try { | ||
Document doc = new Document(SRC); | ||
PyFormatter.formatAll(doc, new MockPyEdit(new MockIFile(ext)), true, black, false, true); | ||
String formattedStr = doc.get(); | ||
|
||
assertEquals(expected, formattedStr); | ||
} catch (SyntaxErrorException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* @see junit.framework.TestCase#setUp() | ||
*/ | ||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
black = new FormatStd(); | ||
black.formatterStyle = FormatterEnum.BLACK; | ||
black.searchBlackInInterpreter = false; | ||
|
||
//TODO: get black location from config? | ||
black.blackExecutableLocation = System.getenv("LOCALAPPDATA") | ||
+ "\\Programs\\Python\\Python37\\Scripts\\black.exe"; | ||
|
||
System.out.println(black); | ||
} | ||
|
||
/** | ||
* Check that py file is formatted using Black's standard format | ||
* @throws Exception | ||
*/ | ||
public void testPyFormat() throws Exception { | ||
checkFormat("py", "from typing import Union, Dict\n" | ||
+ "\n" | ||
+ "\n" | ||
+ "def a() -> None:\n" | ||
+ " ...\n" | ||
+ "\n" | ||
+ "\n" | ||
+ "def b(_: Union[Dict[str, str], str]) -> int:\n" | ||
+ " ...\n" | ||
+ "\n" | ||
+ "\n" | ||
+ "d = [1, 2, 3]\n"); | ||
} | ||
|
||
/** | ||
* Check that pyi (python stub) file is formatted using Black's more compact format | ||
* @throws Exception | ||
*/ | ||
public void testPyiFormat() throws Exception { | ||
checkFormat("py", "from typing import Union, Dict\n" | ||
+ "\n" | ||
+ "def a() -> None: ...\n" | ||
+ "def b(_: Union[Dict[str, str], str]) -> int: ...\n" | ||
+ "\n" | ||
+ "d = [1, 2, 3]\n"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure the best way to get black executable when running unit tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be based on something from
org.python.pydev.core.TestDependent
.The values there are computed when TestDependent is imported.
You can create a file custom for your machine by setting the environment variable "PYDEV_TEST_PLATFORM" to some value -- say:
zeckie
-- and then create aTestDependent.zeckie.properties
file based on the values of one of the existing templates there.Ideally it should be a new entry there with a default value computed based on the
TestDependent.PYTHON_INSTALL
(to do that, change thestatic{}
block onTestDependent
to fill the value if it wasn't loaded).