Skip to content
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

Support python stub files #52

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/python/PythonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private static com.jetbrains.python.psi.LanguageLevel mapLanguageLevel(LanguageL

@Override
public boolean accept(Path path) {
return path.toString().endsWith(".py");
String pathString = path.toString();
return pathString.endsWith(".py") || pathString.endsWith(".pyi");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2320,7 +2320,7 @@ public J.Literal mapNoneLiteral(PyNoneLiteralExpression element) {
spaceBefore(element),
EMPTY,
null,
"null",
element.isEllipsis() ? "..." : "null",
null,
JavaType.Primitive.Null
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,9 @@ public J visitLiteral(J.Literal literal, PrintOutputCapture<P> p) {
if (literal.getMarkers().findFirst(ImplicitNone.class).isPresent()) {
literal = literal.withValueSource("");
} else {
literal = literal.withValueSource("None");
if ("null".equals(literal.getValueSource())) {
literal = literal.withValueSource("None");
}
}
}

Expand Down
253 changes: 139 additions & 114 deletions src/test/java/org/openrewrite/python/tree/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,138 +16,163 @@
package org.openrewrite.python.tree;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.Issue;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.python.tree.ParserAssertions.python;
import org.openrewrite.test.SourceSpecs;

class ImportTest implements RewriteTest {

@ParameterizedTest
//language=py
@ValueSource(strings = {
"import math",
"import math",
})
void simpleImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@ParameterizedTest
//language=py
@ValueSource(strings = {
"import math as math2",
"import math as math2",
"import math as math2",
"import math as math2",
})
void simpleImportAlias(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}
abstract class Base {

abstract SourceSpecs python(@Language("py") @Nullable String before);

@ParameterizedTest
//language=py
@ValueSource(strings = {
"from . import foo",
"from . import foo",
"from . import foo",
"from . import foo",
"from .mod import foo",
"from .mod import foo",
"from .mod import foo",
"from .mod import foo",
"from ...mod import foo",
"from ....mod import foo",
})
void localImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}
@ParameterizedTest
//language=py
@ValueSource(strings = {
"import math",
"import math",
})
void simpleImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@ParameterizedTest
//language=py
@ValueSource(strings = {
"from math import ceil",
"from math import ceil",
"from math import ceil",
"from math import ceil",
})
void qualifiedImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}
@ParameterizedTest
//language=py
@ValueSource(strings = {
"import math as math2",
"import math as math2",
"import math as math2",
"import math as math2",
})
void simpleImportAlias(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@ParameterizedTest
//language=py
@ValueSource(strings = {
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
})
void localImportAlias(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@Issue("https://github.com/openrewrite/rewrite-python/issues/35")
@Test
void multipleImports() {
rewriteRun(
python(
"""
import sys

import math
""")
);
@ParameterizedTest
//language=py
@ValueSource(strings = {
"from . import foo",
"from . import foo",
"from . import foo",
"from . import foo",
"from .mod import foo",
"from .mod import foo",
"from .mod import foo",
"from .mod import foo",
"from ...mod import foo",
"from ....mod import foo",
})
void localImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@ParameterizedTest
//language=py
@ValueSource(strings = {
"from math import ceil",
"from math import ceil",
"from math import ceil",
"from math import ceil",
})
void qualifiedImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@ParameterizedTest
//language=py
@ValueSource(strings = {
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
"from . import foo as foo2",
})
void localImportAlias(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}

@Issue("https://github.com/openrewrite/rewrite-python/issues/35")
@Test
void multipleImports() {
rewriteRun(
python(
"""
import sys

import math
""")
);
}

@Issue("https://github.com/openrewrite/rewrite-python/issues/35")
@Test
void enclosedInParens() {
rewriteRun(
python(
"""
from math import (
sin,
cos
)
""")
);
}

@SuppressWarnings("TrailingWhitespacesInTextBlock")
@ParameterizedTest
//language=py
@ValueSource(strings = {
"from math import sin, cos # stuff\n\n",
"from math import sin as sin2, cos\n",
"from math import sin as sin2, cos as cos2\n",
"""
from math import (
sin,
cos
)
""",
})
void multipleImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
}
}

@Issue("https://github.com/openrewrite/rewrite-python/issues/35")
@Test
void enclosedInParens() {
rewriteRun(
python(
"""
from math import (
sin,
cos
)
""")
);
@Nested
class PythonFile extends Base {

@Override
SourceSpecs python(@Nullable String before) {
return org.openrewrite.python.tree.ParserAssertions.python(before);
}
}

@SuppressWarnings("TrailingWhitespacesInTextBlock")
@ParameterizedTest
//language=py
@ValueSource(strings = {
"from math import sin, cos # stuff\n\n",
"from math import sin as sin2, cos\n",
"from math import sin as sin2, cos as cos2\n",
"""
from math import (
sin,
cos
)
""",
})
void multipleImport(@Language("py") String arg) {
rewriteRun(
python(arg)
);
@Nested
class PythonStub extends Base {

@Override
SourceSpecs python(@Nullable String before) {
return org.openrewrite.python.tree.ParserAssertions.python(before, spec -> spec.path("file.pyi"));
}
}

}
44 changes: 44 additions & 0 deletions src/test/java/org/openrewrite/python/tree/StubTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.python.Assertions.python;

public class StubTest implements RewriteTest {

@Test
void simpleStub() {
rewriteRun(
python("""
# Variables with annotations do not need to be assigned a value.
# So by convention, we omit them in the stub file.
x: int

# Function bodies cannot be completely removed. By convention,
# we replace them with `...` instead of the `pass` statement.
def func_1(code: str) -> int: ...

# We can do the same with default arguments.
def func_2(a: int, b: int = ...) -> int: ...
""",
spec -> spec.path("file.pyi")
)
);
}
}