Skip to content

Commit f3c2a9b

Browse files
authored
Merge pull request #110 from KotlinIsland/fix-intellij
intellij: fix narrowing
2 parents b115d91 + 98f770d commit f3c2a9b

File tree

20 files changed

+1083
-42
lines changed

20 files changed

+1083
-42
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
intellij/src/test/java/** linguist-vendored

.github/workflows/check.yaml

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ name: check
22

33
on: push
44

5-
env:
6-
POETRY_VERSION: "1.7.1"
7-
85
jobs:
96
check:
107
runs-on: ubuntu-latest
@@ -27,11 +24,7 @@ jobs:
2724
- uses: actions/setup-python@v5
2825
with:
2926
python-version: ${{ matrix.python-version }}
30-
- name: Run image
31-
uses: abatilo/[email protected]
32-
with:
33-
poetry-version: ${{ env.POETRY_VERSION }}
34-
- run: poetry config virtualenvs.in-project true
27+
- run: ./pw poetry config virtualenvs.in-project true
3528
- name: Set up cache
3629
uses: actions/cache@v3
3730
id: cache
@@ -41,9 +34,9 @@ jobs:
4134
- name: Ensure cache is healthy
4235
if: steps.cache.outputs.cache-hit == 'true'
4336
run: timeout 10s poetry run pip --version || rm -rf .venv
44-
- run: poetry install
45-
- run: poetry run mypy -p basedtyping -p tests --python-version ${{ matrix.usable-python-version }}
46-
- run: poetry run pytest tests/
37+
- run: ./pw poetry install
38+
- run: ./pw poetry run mypy -p basedtyping -p tests --python-version ${{ matrix.usable-python-version }}
39+
- run: ./pw poetry run pytest tests/
4740

4841
lint:
4942
runs-on: ubuntu-latest
@@ -52,11 +45,7 @@ jobs:
5245
- uses: actions/setup-python@v5
5346
with:
5447
python-version: "3.8"
55-
- name: Run image
56-
uses: abatilo/[email protected]
57-
with:
58-
poetry-version: ${{ env.POETRY_VERSION }}
59-
- run: poetry config virtualenvs.in-project true
48+
- run: ./pw poetry config virtualenvs.in-project true
6049
- name: Set up cache
6150
uses: actions/cache@v3
6251
id: cache
@@ -65,7 +54,7 @@ jobs:
6554
key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
6655
- name: Ensure cache is healthy
6756
if: steps.cache.outputs.cache-hit == 'true'
68-
run: timeout 10s poetry run pip --version || rm -rf .venv
69-
- run: poetry install
70-
- run: poetry run ruff format --check --diff
71-
- run: poetry run ruff check
57+
run: timeout 10s ./pw poetry run pip --version || rm -rf .venv
58+
- run: ./pw poetry install
59+
- run: ./pw poetry run ruff format --check --diff
60+
- run: ./pw poetry run ruff check

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
**/__pycache__
22
/.venv/
33
/venv/
4-
/dist/
4+
/dist/
5+
/.pyprojectx

.idea/basedtyping.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

intellij/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
## [Unreleased]
66

7+
## [0.1.3]
8+
79
### Added
810
- Callable literal types `() -> int`
11+
### Fixed
12+
- Narrowed types were broken
913

1014
## [0.1.2] - 2023-01-12
1115

intellij/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ dependencies {
2626

2727
// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
2828
kotlin {
29-
@Suppress("UnstableApiUsage")
3029
jvmToolchain {
3130
languageVersion = JavaLanguageVersion.of(17)
31+
@Suppress("UnstableApiUsage")
3232
vendor = JvmVendorSpec.JETBRAINS
3333
}
3434
}

intellij/gradle.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ pluginGroup = org.basedsoft.plugins.basedtyping
44
pluginName = basedtyping
55
pluginRepositoryUrl = https://github.com/KotlinIsland/basedtyping
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.1.2
7+
pluginVersion = 0.1.3
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 233
11-
pluginUntilBuild = 233.*
11+
pluginUntilBuild = 241.*
1212

1313
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
14-
platformType = IC
15-
platformVersion = 2023.3.2
14+
platformType = PC
15+
platformVersion = 2023.3.3
1616

1717
# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
1818
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
19-
platformPlugins = PythonCore:233.11799.300
19+
platformPlugins = PythonCore
2020

2121
# Gradle Releases -> https://github.com/gradle/gradle/releases
22-
gradleVersion = 8.5
22+
gradleVersion = 8.6
2323

2424
# Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib
2525
kotlin.stdlib.default.dependency = false

intellij/gradle/libs.versions.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
annotations = "24.1.0"
44

55
# plugins
6-
kotlin = "1.9.21"
6+
kotlin = "1.9.22"
77
changelog = "2.2.0"
8-
gradleIntelliJPlugin = "1.16.1"
8+
gradleIntelliJPlugin = "1.17.1"
99
kover = "0.7.5"
1010

11+
1112
[libraries]
1213
annotations = { group = "org.jetbrains", name = "annotations", version.ref = "annotations" }
1314

intellij/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

intellij/gradlew.bat

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
4343
%JAVA_EXE% -version >NUL 2>&1
4444
if %ERRORLEVEL% equ 0 goto execute
4545

46-
echo.
47-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48-
echo.
49-
echo Please set the JAVA_HOME variable in your environment to match the
50-
echo location of your Java installation.
46+
echo. 1>&2
47+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
48+
echo. 1>&2
49+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
50+
echo location of your Java installation. 1>&2
5151

5252
goto fail
5353

@@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
5757

5858
if exist "%JAVA_EXE%" goto execute
5959

60-
echo.
61-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62-
echo.
63-
echo Please set the JAVA_HOME variable in your environment to match the
64-
echo location of your Java installation.
60+
echo. 1>&2
61+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
62+
echo. 1>&2
63+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
64+
echo location of your Java installation. 1>&2
6565

6666
goto fail
6767

intellij/src/main/kotlin/org/basedsoft/plugins/basedtyping/BasedTypingTypeProvider.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private class BasedTypingTypeProvider : PyTypeProviderBase() {
4545
val param = referenceExpression.followAssignmentsChain(PyResolveContext.defaultContext(context)).element
4646
if (param !is PyNamedParameter) return null
4747
val annotation = param.annotation?.value ?: return null
48-
return getType(annotation, context)
48+
return getType(annotation, context, simple=true)
4949
}
5050
/**
5151
* Needed to work around a limitation in PyTypingTypeProvider
@@ -137,6 +137,7 @@ fun getOverload(param: PyNamedParameter, func: PyFunction, context: TypeEvalCont
137137
.filterNotNull()
138138
.toSet()
139139
.takeIf { it.isNotEmpty() }
140+
// TODO: keep the correct order
140141
?.let { PyUnionType.union(it) }
141142

142143
fun getOverloadReturn(func: PyFunction, context: TypeEvalContext) =
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2+
package com.jetbrains.python;
3+
4+
import com.intellij.openapi.application.Application;
5+
import com.intellij.openapi.application.ApplicationManager;
6+
import com.intellij.openapi.projectRoots.*;
7+
import com.intellij.openapi.roots.OrderRootType;
8+
import com.intellij.openapi.util.KeyWithDefaultValue;
9+
import com.intellij.openapi.vfs.LocalFileSystem;
10+
import com.intellij.openapi.vfs.VirtualFile;
11+
import com.intellij.util.containers.ContainerUtil;
12+
import com.jetbrains.python.codeInsight.typing.PyTypeShed;
13+
import com.jetbrains.python.codeInsight.userSkeletons.PyUserSkeletonsUtil;
14+
import com.jetbrains.python.psi.LanguageLevel;
15+
import com.jetbrains.python.sdk.PythonSdkUtil;
16+
import org.jdom.Element;
17+
import org.jetbrains.annotations.NonNls;
18+
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
20+
21+
import java.io.File;
22+
import java.util.ArrayList;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
26+
public final class PythonMockSdk {
27+
28+
private PythonMockSdk() {
29+
}
30+
31+
public static @NotNull Sdk create() {
32+
return create(LanguageLevel.getLatest());
33+
}
34+
35+
public static @NotNull Sdk create(@NotNull String sdkPath) {
36+
return create(sdkPath, LanguageLevel.getLatest());
37+
}
38+
39+
public static @NotNull Sdk create(@NotNull LanguageLevel level, VirtualFile @NotNull ... additionalRoots) {
40+
return create(PythonHelpersLocator.getPythonCommunityPath() + "/testData" + "/MockSdk", level, additionalRoots);
41+
}
42+
43+
private static @NotNull Sdk create(@NotNull String sdkPath, @NotNull LanguageLevel level, VirtualFile @NotNull ... additionalRoots) {
44+
String sdkName = "Mock " + PyNames.PYTHON_SDK_ID_NAME + " " + level.toPythonVersion();
45+
return create(sdkName, sdkPath, new PyMockSdkType(level), level, additionalRoots);
46+
}
47+
48+
public static @NotNull Sdk create(@NotNull String sdkName,
49+
@NotNull String sdkPath,
50+
@NotNull SdkTypeId sdkType,
51+
@NotNull LanguageLevel level,
52+
VirtualFile @NotNull ... additionalRoots) {
53+
Sdk sdk = ProjectJdkTable.getInstance().createSdk(sdkName, sdkType);
54+
SdkModificator sdkModificator = sdk.getSdkModificator();
55+
sdkModificator.setHomePath(sdkPath + "/bin/python");
56+
sdkModificator.setVersionString(toVersionString(level));
57+
58+
createRoots(sdkPath, level).forEach(vFile -> {
59+
sdkModificator.addRoot(vFile, OrderRootType.CLASSES);
60+
});
61+
62+
Arrays.asList(additionalRoots).forEach(vFile -> {
63+
sdkModificator.addRoot(vFile, OrderRootType.CLASSES);
64+
});
65+
66+
Application application = ApplicationManager.getApplication();
67+
Runnable runnable = () -> sdkModificator.commitChanges();
68+
if (application.isDispatchThread()) {
69+
application.runWriteAction(runnable);
70+
} else {
71+
application.invokeAndWait(() -> application.runWriteAction(runnable));
72+
}
73+
sdk.putUserData(KeyWithDefaultValue.create("MOCK_PY_MARKER_KEY", true), true);
74+
return sdk;
75+
76+
// com.jetbrains.python.psi.resolve.PythonSdkPathCache.getInstance() corrupts SDK, so have to clone
77+
//return sdk.clone();
78+
}
79+
80+
private static @NotNull List<VirtualFile> createRoots(@NotNull @NonNls String mockSdkPath, @NotNull LanguageLevel level) {
81+
final var result = new ArrayList<VirtualFile>();
82+
83+
final var localFS = LocalFileSystem.getInstance();
84+
ContainerUtil.addIfNotNull(result, localFS.refreshAndFindFileByIoFile(new File(mockSdkPath, "Lib")));
85+
ContainerUtil.addIfNotNull(result, localFS.refreshAndFindFileByIoFile(new File(mockSdkPath, PythonSdkUtil.SKELETON_DIR_NAME)));
86+
87+
ContainerUtil.addIfNotNull(result, PyUserSkeletonsUtil.getUserSkeletonsDirectory());
88+
89+
result.addAll(PyTypeShed.INSTANCE.findRootsForLanguageLevel(level));
90+
91+
return result;
92+
}
93+
94+
private static @NotNull String toVersionString(@NotNull LanguageLevel level) {
95+
return "Python " + level.toPythonVersion();
96+
}
97+
98+
private static final class PyMockSdkType implements SdkTypeId {
99+
100+
@NotNull
101+
private final LanguageLevel myLevel;
102+
103+
private PyMockSdkType(@NotNull LanguageLevel level) {
104+
myLevel = level;
105+
}
106+
107+
@NotNull
108+
@Override
109+
public String getName() {
110+
return PyNames.PYTHON_SDK_ID_NAME;
111+
}
112+
113+
@Override
114+
public @NotNull String getVersionString(@NotNull Sdk sdk) {
115+
return toVersionString(myLevel);
116+
}
117+
118+
@Override
119+
public void saveAdditionalData(@NotNull SdkAdditionalData additionalData, @NotNull Element additional) {
120+
}
121+
122+
@Nullable
123+
@Override
124+
public SdkAdditionalData loadAdditionalData(@NotNull Sdk currentSdk, @NotNull Element additional) {
125+
return null;
126+
}
127+
}
128+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This stuff is copied from JetBrains/intellij-community because they don't ship it

0 commit comments

Comments
 (0)