Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions python/pyspark/ml/param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,17 @@ def isDefined(self, param):
return self.isSet(param) or self.hasDefault(param)

@since("1.4.0")
def hasParam(self, paramName):
def hasParam(self, param):
"""
Tests whether this instance contains a param with a given
(string) name.
Tests whether this instance contains a param.
"""
param = self._resolveParam(paramName)
return param in self.params
if isinstance(param, Param):
return hasattr(self, param.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we support param of type Param, we should not only check the hasattr(self, param.name) but also check self.uid == param.parent. You can directly call _shouldOwn to do this work. It means if you provide a Param to check whether it belongs to the instance, you should both check uid and name.
But I vote to only support paramName that make consistent semantics with Scala.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to agree that we should only accept string types for this function. The reason I have included Param type is because in the current ml param tests, there is a check where hasParam is called by passing a Param instance instead of a string, so this test would fail (see here). It is odd that the test passes a Param instance and not a string, since the function describes itself as accepting strings, but, in an odd twist, the check works anyway.

If we do accept Param type, we can't call _shouldOwn because it throws an error instead of returning False (by design?). At any rate, I vote to accept only strings and change the test to pass in the param name instead of the param.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for accepting only strings. If no strong reasons, keep consistent with Scala is the best choice.

elif isinstance(param, str):
p = getattr(self, param, None)
return p is not None and isinstance(p, Param)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: p is not None is not needed

else:
raise TypeError("hasParam(): param must be a string or Param type")

@since("1.4.0")
def getOrDefault(self, param):
Expand Down