Skip to content

Generate __hash__ on python as well #76

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

Merged
merged 4 commits into from
Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ compileUnionTag source parentname typename' fields = do
slots = if length tagNames == 1
then [qq|'{head tagNames}'|] `T.snoc` ','
else toIndentedCodes (\n -> [qq|'{n}'|]) tagNames ",\n "
hashTuple = if null tagNames
then "self.__nirum_tag__"
else [qq|({attributes},)|] :: T.Text
where
attributes :: T.Text
attributes = toIndentedCodes (\n -> [qq|self.{n}|]) tagNames ", "
initialArgs = toIndentedCodes
(\(n, t) -> [qq|{n}: {t}|]) nameNTypes ", "
initialValues =
Expand Down Expand Up @@ -244,6 +250,9 @@ class $className($parentClass):
getattr(self, attr) == getattr(other, attr)
for attr in self.__slots__
)

def __hash__(self) -> int:
return hash($hashTuple)
|]

compilePrimitiveType :: PrimitiveTypeIdentifier -> CodeGen Code
Expand Down Expand Up @@ -338,6 +347,9 @@ class $className:
return '\{0.__module__\}.\{0.__qualname__\}(\{1!r\})'.format(
type(self), self.value
)

def __hash__(self) -> int:
return hash(self.value)
|]
compileTypeDeclaration _ TypeDeclaration { typename = typename'
, type' = EnumType members } = do
Expand Down Expand Up @@ -381,6 +393,9 @@ compileTypeDeclaration src TypeDeclaration { typename = typename'
toNamePair
[name | Field name _ _ <- toList fields]
",\n "
hashTuple = [qq|({attributes},)|] :: T.Text
where
attributes = toIndentedCodes (\n -> [qq|self.{n}|]) fieldNames ","
insertStandardImport "typing"
insertThirdPartyImports [ ("nirum.validate", ["validate_record_type"])
, ("nirum.serialize", ["serialize_record_type"])
Expand Down Expand Up @@ -425,6 +440,9 @@ class $className:
@classmethod
def __nirum_deserialize__(cls: type, value) -> '{className}':
return deserialize_record_type(cls, value)

def __hash__(self) -> int:
return hash($hashTuple)
|]
compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = UnionType tags } = do
Expand Down
9 changes: 7 additions & 2 deletions test/Nirum/Targets/PythonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ spec = parallel $ do
tT decl "FloatBox(3.14).__nirum_serialize__() == 3.14"
tT decl "FloatBox.__nirum_deserialize__(3.14) == FloatBox(3.14)"
tT decl "FloatBox.__nirum_deserialize__(3.14) == FloatBox(3.14)"
tT decl "hash(FloatBox(3.14))"
tT decl "hash(FloatBox(3.14)) != 3.14"
tR' decl "TypeError" "FloatBox.__nirum_deserialize__('a')"
tR' decl "TypeError" "FloatBox('a')"
let decls = [ Import ["foo", "bar"] "path-box" empty
Expand Down Expand Up @@ -596,6 +598,7 @@ spec = parallel $ do
tT decl "Point(left=3, top=14) != Point(left=4, top=14)"
tT decl "Point(left=3, top=14) != Point(left=4, top=15)"
tT decl "Point(left=3, top=14) != 'foo'"
tT decl "hash(Point(left=3, top=14))"
tT decl [q|Point(left=3, top=14).__nirum_serialize__() ==
{'_type': 'point', 'x': 3, 'top': 14}|]
tT decl [qq|Point.__nirum_deserialize__($payload) ==
Expand Down Expand Up @@ -669,7 +672,7 @@ spec = parallel $ do
tT decl "Line.__slots__ == ('length', )"
tT decl [qq|Line(length=3).__nirum_serialize__() == $payload|]
specify "union type" $ do
let wasternNameTag =
let westernNameTag =
Tag "western-name" [ Field "first-name" "text" empty
, Field "middle-name" "text" empty
, Field "last-name" "text" empty
Expand All @@ -682,7 +685,7 @@ spec = parallel $ do
Tag "culture-agnostic-name"
[ Field "fullname" "text" empty ]
empty
tags = [ wasternNameTag
tags = [ westernNameTag
, eastAsianNameTag
, cultureAgnosticNameTag
]
Expand Down Expand Up @@ -733,6 +736,8 @@ spec = parallel $ do
last_name='wrong') !=
WesternName(first_name='foo', middle_name='bar',
last_name='baz')|]
tT decl [q|hash(WesternName(first_name='foo', middle_name='bar',
last_name='baz'))|]
tT decl "isinstance(EastAsianName, type)"
tT decl "issubclass(EastAsianName, Name)"
tT decl [q|EastAsianName(family_name='foo',
Expand Down