mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-22730 Properly render Union and Optional types containing TypeVars
Namely, don't consider them "unknown" wrapping them in another weak Union and rendering using PyType#getName() rather than PyTypeModelBuilder.
This commit is contained in:
@@ -266,7 +266,7 @@ public class PyTypeModelBuilder {
|
||||
}
|
||||
else if (type instanceof PyUnionType && allowUnions) {
|
||||
final PyUnionType unionType = (PyUnionType)type;
|
||||
if (type instanceof PyDynamicallyEvaluatedType || PyTypeChecker.isUnknown(type)) {
|
||||
if (type instanceof PyDynamicallyEvaluatedType || PyTypeChecker.isUnknown(type, false)) {
|
||||
result = new UnknownType(build(unionType.excludeNull(myContext), true));
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -346,13 +346,17 @@ public class PyTypeChecker {
|
||||
}
|
||||
|
||||
public static boolean isUnknown(@Nullable PyType type) {
|
||||
if (type == null || type instanceof PyGenericType) {
|
||||
return isUnknown(type, true);
|
||||
}
|
||||
|
||||
public static boolean isUnknown(@Nullable PyType type, boolean genericsAreUnknown) {
|
||||
if (type == null || (genericsAreUnknown && type instanceof PyGenericType)) {
|
||||
return true;
|
||||
}
|
||||
if (type instanceof PyUnionType) {
|
||||
final PyUnionType union = (PyUnionType)type;
|
||||
for (PyType t : union.getMembers()) {
|
||||
if (isUnknown(t)) {
|
||||
if (isUnknown(t, genericsAreUnknown)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<html><body><code>def <b>f</b>(x1: Optional[T], x2: Union[T, Tuple[Any, Any]])<br>Inferred type: (x1: Optional[TypeVar('T', <a href="psi_element://#typename#int">int</a>)], x2: Union[TypeVar('T', <a href="psi_element://#typename#int">int</a>), Tuple[Any, Any]]) -> None<br></code></body></html>
|
||||
@@ -0,0 +1,11 @@
|
||||
from typing import TypeVar, Optional, Union, Tuple, Any
|
||||
|
||||
|
||||
T = TypeVar('T', int)
|
||||
|
||||
|
||||
def f(x1: Optional[T], x2: Union[T, Tuple[Any, Any]]):
|
||||
print(x1, x2)
|
||||
|
||||
|
||||
<the_ref>f
|
||||
@@ -282,4 +282,10 @@ public class PyQuickDocTest extends LightMarkedTestCase {
|
||||
myFixture.copyDirectoryToProject("typing", "");
|
||||
runWithLanguageLevel(LanguageLevel.PYTHON35, this::checkHTMLOnly);
|
||||
}
|
||||
|
||||
// PY-22730
|
||||
public void testOptionalAndUnionTypesContainingTypeVars() {
|
||||
myFixture.copyDirectoryToProject("typing", "");
|
||||
runWithLanguageLevel(LanguageLevel.PYTHON36, this::checkHTMLOnly);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user