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

Prevent unions with itself #1997

Merged
merged 36 commits into from Apr 17, 2020
Merged

Prevent unions with itself #1997

merged 36 commits into from Apr 17, 2020

Conversation

@MikhailArkhipov
Copy link
Member

@MikhailArkhipov MikhailArkhipov commented Apr 17, 2020

Fixes #1985

image

@@ -28,15 +28,19 @@ internal sealed class PythonUnionType : LocatedMember, IPythonUnionType {

public PythonUnionType(IEnumerable<IPythonType> types, IPythonModule declaringModule)
: base(declaringModule.Interpreter.ModuleResolution.GetSpecializedModule("typing")) {
_types.UnionWith(types);
_types.UnionWith(types.Where(t => !this.Equals(t)));

This comment has been minimized.

@jakebailey

jakebailey Apr 17, 2020
Member

Do either of the constructors end up mattering? Or is it just Add that was the problem? I'm not sure how you could end up ever having an object "equal" to the current one if you're in the constructor for this, but I could be wrong.

This comment has been minimized.

@MikhailArkhipov

MikhailArkhipov Apr 17, 2020
Author Member

It basically gets confused on

    def get_if_str(self, obj: Union[str, Factory]):
        """
        Returns object from the registry if ``obj`` type is string.
        """
        if type(obj) is str:
            return self.get(obj)
        return obj

when calculating return value. Another option is to introduce deep compare to Union so !currentType.Equals(valueType) would trigger. But it would be narrower fix as there may be other calls to Add ending up with circular unions.

            if (!_fromAnnotation && !currentType.Equals(valueType)) {
                var type = PythonUnionType.Combine(currentType, valueType);
                // Track instance vs type info.
                StaticReturnValue = value is IPythonInstance ? type.CreateInstance(ArgumentSet.WithoutContext) : (IMember)type;
            }

This comment has been minimized.

@jakebailey

jakebailey Apr 17, 2020
Member

Hm, okay.

@MikhailArkhipov MikhailArkhipov merged commit 52a2397 into microsoft:master Apr 17, 2020
1 check passed
1 check passed
license/cla All CLA requirements met.
Details
@MikhailArkhipov MikhailArkhipov deleted the MikhailArkhipov:so branch Apr 17, 2020
MikhailArkhipov added a commit that referenced this pull request Jun 15, 2020
* Remove stale reference

* Don't suppress LHS diagnostics on augmented assign

* Revert "Don't suppress LHS diagnostics on augmented assign"

This reverts commit 6109ac7.

* Escape [ and ]

* PR feedback

* Prevent unions with itself

* Add special Union comparison + test

(cherry picked from commit 52a2397)
MikhailArkhipov added a commit that referenced this pull request Jun 15, 2020
* Remove stale reference

* Don't suppress LHS diagnostics on augmented assign

* Revert "Don't suppress LHS diagnostics on augmented assign"

This reverts commit 6109ac7.

* Escape [ and ]

* PR feedback

* Prevent unions with itself

* Add special Union comparison + test

(cherry picked from commit 52a2397)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants
You can’t perform that action at this time.