Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

[WIP] Support Namespace Packages. Part 1: path resolver - #327

Merged
Alexander Sher (AlexanderSher) merged 5 commits into
microsoft:masterfrom
AlexanderSher:NamespacePackages
Nov 6, 2018
Merged

Alexander Sher (AlexanderSher) merged 5 commits into
microsoft:masterfrom
AlexanderSher:NamespacePackages

Conversation

@AlexanderSher

Copy link
Copy Markdown
Contributor

This PR adds a structure that holds references to all python modules in a form of a tree that can be searched for packages and names that are available for each import. This is not equivalent to exact set of names, which is affected by previous imports (see, for instance, #288 (comment)).

@AlexanderSher

Copy link
Copy Markdown
Contributor Author

Test changes will follow

…-server into NamespacePackages

# Conflicts:
#	src/Analysis/Engine/Test/CompletionTests.cs
#	src/LanguageServer/Impl/LanguageServer.cs
}

public int GetChildIndex(string childName) {
for (var index = 0; index < _children.Length; index++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we had IndexWhere in RTVS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll port it.


namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal partial struct PathResolverSnapshot {
private static readonly bool IgnoreCase = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And Mac. Although it is possible to have case-sensitive MacOS FS, it is rare I believe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it can be more complicated: https://www.howtogeek.com/354220/how-to-enable-case-sensitive-folders-on-windows-10/
Or we can simply assume the "80%" case.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess on Windows it is more like 0.01% since setting appears to be per folder.

public PathResolverSnapshot(PythonLanguageVersion pythonLanguageVersion) {
_pythonLanguageVersion = pythonLanguageVersion;
_roots = Array.Empty<Node>();
Version = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default

private readonly Node[] _roots;
public int Version { get; }

public PathResolverSnapshot(PythonLanguageVersion pythonLanguageVersion) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: this(Array.Empty<Node>(), 0) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal partial struct PathResolverSnapshot {
/// <summary>
/// Represents arc between two nodes in the tree

@MikhailArkhipov Mikhail Arkhipov (MikhailArkhipov) Oct 31, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is called edge in https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#edge. Or path since it represents multiple nodes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
but I can rename it if it is easier to understand.

@jakebailey Jake Bailey (jakebailey) Oct 31, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in the same vein of naming changes would be vertexes -> vertices, but I think "arc" is much less common relative to "edge" than "vertexes" is to "vertices".

(EDIT: Ah, Mikhail said that below. Github emails are lagging behind...)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine either way, just that more commonly used term is easier to understand for newcomers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed

return GetAvailableImports(lastArc);
}

private IAvailableImports GetAvailableImports(in Arc lastArc, in bool requireInitPy = true) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need requireInitPy? The object appears to use specific language version that defines the rule.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, not required.

using System;

namespace Microsoft.PythonTools.Analysis.Infrastructure {
internal static class ArrayExtensions {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think System.Collections.Immutable has all of this stuff. Would there be any benefit to using that (specifically ImmutableArray) instead of our own custom extensions to the builtin Array? I'd think it might help make clear what actually has to be immutable (and what doesn't), and it claims to optimize things to prevent unneeded copies when possible.

@AlexanderSher Alexander Sher (AlexanderSher) Oct 31, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ImmutableArray is almost free (it is a thin struct wrapper), but other types are rather slow and expensive. Referencing new package for a single type is an overkill, especially for a reference-free assembly. Also ImmutableArray stops being cheap when any custom change operation is required.
We can grab the sources of the ImmutableArray.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm a bit ignorant of the implications of adding a reference when we currently have none. I'm personally less satisfied by copying the source code of another library rather than using it the normal way (especially since its code seems to be non-trivial), or even leaving it how it is with some indication that uses shouldn't modify it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem may happen if several versions of assembly has to referenced through different code paths. This particular assembly (System.Collections.Immutable) has caused problems in RTVS because of the threading package that required special version of assembly that we couldn't use in our extension.
So being dependent only on .net standard is better for our clients.


namespace Microsoft.PythonTools.Analysis.DependencyResolution {
internal interface IAvailableImports {
string Name { get; }

@MikhailArkhipov Mikhail Arkhipov (MikhailArkhipov) Oct 31, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Name? What about Modules and Packages in AvailablePackageImports? Also, should it be public?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AvailableModuleImports doesn't have Modules and Packages. It only has ModulePath which should be used to list variables that are defined in that module (if AST is available).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I am confused by plural Imports while there is only single Name

// List of vertexes.
// To provide immutability, it must be changed only by calling List<T>.Add
// Better to be replaced with some immutable structure
private readonly List<(int index, Node node)> _vertexes;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vertices

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.


normalizedPath = PathUtils.NormalizePath(modulePath);
var rootIndex = 0;
while (rootIndex < _roots.Length && !normalizedPath.StartsWithOrdinal(_roots[rootIndex].Name)) {

@MikhailArkhipov Mikhail Arkhipov (MikhailArkhipov) Oct 31, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS-specific case sensitivity comparison aka IgnoreCase?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python is case sensitive in terms of names, need to check how it works with root locations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 235 doesn't say anything about root locations. Experiment shows that it depends on file system, so with our approximation we can use OS.

}

private bool TryFindModule(string modulePath, out Arc lastArc, out string normalizedPath, out (int start, int length) unmatchedPathSpan) {
if (!Path.IsPathRooted(modulePath)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it time to introduce IFileSystem for testability along with service container?... So we can run Mac path tests on Windows and the other way around?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. Do you want me to add it to this [WIP]?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, prob will need for tests anyway.

var uri = GetDocument(Path.Combine("TestData", "AstAnalysis", "TopLevelCompletions.py"));
await s.LoadFileAsync(uri);
using (var server = await CreateServerAsync(PythonVersions.LatestAvailable3X)) {
var uri = GetDocument(@"TestData\AstAnalysis\TopLevelCompletions.py");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this path should be getting changed back to \.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, merge conflict.


private IServiceContainer _services;
private IUIService _ui;
private ITelemetryService2 _telemetry;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you're removing this? The RPC trace listener isn't going to be the only thing that needs to access telemetry (and I don't think it has anything to do with this PR's intent).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't like fields that are just hanging around.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Jake is now using it ;-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He moved the GetService down into the constructor call for the class I added, so it'll still function correctly. My statement was more about how if/when we start sending more telemetry than just for RPC method exceptions, then this change will just be undone anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly. If all the usages of ITelemetryService2 instance in LanguageServer is limited to passing it to another constructors, we won't need a field as well. We will need a field only if LanguageServer actually uses the ITelemetryService2, but for now it isn't used.

// List of vertexes.
// To provide immutability, it must be changed only by calling List<T>.Add
// Better to be replaced with some immutable structure
private readonly List<(int index, Node node)> _vertexes;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following offline discussion - comment may come handy what is that index represents and how it is used since structure is a bit different from typical node with children.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will add it. And add a DebuggerDisplay as well.

- Fix unit tests
- Some refactorings
# Conflicts:
#	src/Analysis/Engine/Impl/Parsing/Ast/PythonNameBinder.cs
#	src/Analysis/Engine/Impl/PythonAnalyzer.cs
#	src/Analysis/Engine/Test/AstAnalysisTests.cs
@AlexanderSher
Alexander Sher (AlexanderSher) merged commit 509993f into microsoft:master Nov 6, 2018
Jake Bailey (jakebailey) pushed a commit to jakebailey/python-language-server that referenced this pull request Nov 1, 2019
[WIP] Support Namespace Packages. Part 1: path resolver
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

3 participants