From 87f0be9f32c2a40082536a1241beabf5fe552cdd Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Fri, 17 Jun 2016 13:55:51 +0300 Subject: [PATCH] PY-17705 Fixed: Awkward formatting of parameter types in Quick Documentation for io.open() Don't forget to cache type if it was converted to Optional --- .../documentation/PyTypeModelBuilder.java | 23 +++++-------------- .../quickdoc/OptionalParameterType.html | 1 + .../quickdoc/OptionalParameterType.py | 16 +++++++++++++ .../com/jetbrains/python/PyQuickDocTest.java | 7 +++++- 4 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 python/testData/quickdoc/OptionalParameterType.html create mode 100644 python/testData/quickdoc/OptionalParameterType.py diff --git a/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java b/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java index 7a71fa1f443e..b9e7255f79e9 100644 --- a/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java +++ b/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,6 @@ */ package com.jetbrains.python.documentation; -import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.google.common.collect.Maps; import com.intellij.psi.PsiElement; @@ -26,10 +25,7 @@ import com.jetbrains.python.toolbox.ChainIterable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.jetbrains.python.documentation.DocumentationBuilderKit.$; import static com.jetbrains.python.documentation.DocumentationBuilderKit.combUp; @@ -227,17 +223,10 @@ public class PyTypeModelBuilder { result = new UnknownType(build(unionType.excludeNull(myContext), true)); } else { - final PyType optionalType = getOptionalType(unionType); - if (optionalType != null) { - return new OptionalType(build(optionalType, true)); - } - - result = new OneOf(Collections2.transform(unionType.getMembers(), new Function() { - @Override - public TypeModel apply(PyType t) { - return build(t, false); - } - })); + result = Optional + .ofNullable(getOptionalType(unionType)) + .map(optionalType -> new OptionalType(build(optionalType, true))) + .orElseGet(() -> new OneOf(Collections2.transform(unionType.getMembers(), t -> build(t, false)))); } } else if (type instanceof PyCallableType && !(type instanceof PyClassLikeType)) { diff --git a/python/testData/quickdoc/OptionalParameterType.html b/python/testData/quickdoc/OptionalParameterType.html new file mode 100644 index 000000000000..9ec3d77a8dcc --- /dev/null +++ b/python/testData/quickdoc/OptionalParameterType.html @@ -0,0 +1 @@ +def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Inferred type: (file: str, mode: str, buffering: Integral, encoding: Optional[str], errors: Optional[str], newline: Optional[str], closefd: bool, opener: Optional[(str, int) -> int]) -> Union[FileIO[str], TextIOWrapper[unicode]]

This is an alias for the builtin open() function.

\ No newline at end of file diff --git a/python/testData/quickdoc/OptionalParameterType.py b/python/testData/quickdoc/OptionalParameterType.py new file mode 100644 index 000000000000..00006b94069a --- /dev/null +++ b/python/testData/quickdoc/OptionalParameterType.py @@ -0,0 +1,16 @@ +def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None): + """This is an alias for the builtin open() function. + + :type file: str + :type mode: str + :type buffering: numbers.Integral + :type encoding: str | None + :type errors: str | None + :type newline: str | None + :type closefd: bool + :type opener: ((str, int) -> int) | None + :rtype: io.FileIO[bytes] | io.TextIOWrapper[unicode] + """ + pass + +open("service") \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyQuickDocTest.java b/python/testSrc/com/jetbrains/python/PyQuickDocTest.java index 7bc8ac58c5f2..45d69d2d660d 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickDocTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickDocTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -260,4 +260,9 @@ public class PyQuickDocTest extends LightMarkedTestCase { myFixture.copyDirectoryToProject("/quickdoc/" + getTestName(false), ""); checkHover(); } + + // PY-17705 + public void testOptionalParameterType() { + checkHTMLOnly(); + } }