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

Tweaks to --strict-equality based on user feedback #6674

Merged
merged 8 commits into from Apr 27, 2019

Conversation

@ilevkivskyi
Copy link
Collaborator

commented Apr 14, 2019

Fixes #6607
Fixes #6608

@JukkaL
Copy link
Collaborator

left a comment

Thanks! This should make --strict-equality generate many fewer false positives. Left some comments about minor things. You can merge this once you've addressed the feedback.

from typing import Union
x: Union[bytes, str]

b'abc' in x

This comment has been minimized.

Copy link
@JukkaL

JukkaL Apr 15, 2019

Collaborator

b'a' in 'b' fails at runtime. Should this generate an error?

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Apr 27, 2019

Author Collaborator

Yes, but this is independent of this flag. The error message says "Non-overlapping container check ..." while in this example the check may return True. I think this can be tightened in typeshed, we can just define str.__contains__ as accepting str, because 42 in 'b' etc. all fail as well at runtime.

return False

def dangerous_comparison(self, left: Type, right: Type,
original_cont_type: Optional[Type] = None) -> bool:

This comment has been minimized.

Copy link
@JukkaL

JukkaL Apr 15, 2019

Collaborator

Nit: original_cont_type was not immediately clear. Maybe rename to original_container or container_type?

# TODO: support other types (see has_member())?
return False

def has_bytes_component(self, typ: Type) -> bool:

This comment has been minimized.

Copy link
@JukkaL

JukkaL Apr 15, 2019

Collaborator

Style nit: This should be a module-level function.

@@ -1974,9 +1980,33 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
assert result is not None
return result

def dangerous_comparison(self, left: Type, right: Type) -> bool:
def custom_equality_method(self, typ: Type) -> bool:

This comment has been minimized.

Copy link
@JukkaL

JukkaL Apr 15, 2019

Collaborator

Style nit: This should be a module-level function, since it doesn't depend on self.

if method and method.info:
return not method.info.fullname().startswith('builtins.')
return False
# TODO: support other types (see has_member())?

This comment has been minimized.

Copy link
@JukkaL

JukkaL Apr 15, 2019

Collaborator

I think that it would be good to support some additional types -- at least TupleType (use fallback). Maybe Any should return true as well?

"""Does this type have a custom __eq__() method?"""
if isinstance(typ, UnionType):
return any(self.custom_equality_method(t) for t in typ.items)
if isinstance(typ, Instance):

This comment has been minimized.

Copy link
@JukkaL

JukkaL Apr 15, 2019

Collaborator

A really minor nit: maybe handle Instance first, since it's probably the most common case.

@JukkaL

JukkaL approved these changes Apr 27, 2019

Copy link
Collaborator

left a comment

Looks good now. Thanks for the updates!

@ilevkivskyi ilevkivskyi merged commit 2baebda into python:master Apr 27, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@ilevkivskyi ilevkivskyi deleted the ilevkivskyi:tweak-strict-equality branch Apr 27, 2019

@ilevkivskyi

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 27, 2019

Opened python/typeshed#2937 for the str.__contains__() issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.