Skip to content

Commit

Permalink
Fix missing import typing on Python 2
Browse files Browse the repository at this point in the history
Fix #93
  • Loading branch information
dahlia committed Mar 14, 2017
1 parent 0ed249b commit 3f66839
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ insertLocalImport module' object = ST.modify insert'
insert' c@CodeGenContext { localImports = li } =
c { localImports = M.insertWith S.union module' [object] li }

insertTypingImport :: CodeGen ()
insertTypingImport = do

importTypingForPython3 :: CodeGen ()
importTypingForPython3 = do
pyVer <- getPythonVersion
case pyVer of
Python2 -> return ()
Expand Down Expand Up @@ -261,11 +262,12 @@ quote s = [qq|'{s}'|]

typeReprCompiler :: CodeGen (Code -> Code)
typeReprCompiler = do
insertTypingImport
ver <- getPythonVersion
return $ case ver of
Python2 -> \ t -> [qq|($t.__module__ + '.' + $t.__name__)|]
Python3 -> \ t -> [qq|typing._type_repr($t)|]
case ver of
Python2 -> return $ \ t -> [qq|($t.__module__ + '.' + $t.__name__)|]
Python3 -> do
insertStandardImport "typing"
return $ \ t -> [qq|typing._type_repr($t)|]

type ParameterName = Code
type ParameterType = Code
Expand Down Expand Up @@ -315,7 +317,6 @@ compileUnionTag source parentname typename' fields = do
(map fieldName $ toList fields)
",\n "
parentClass = toClassName' parentname
insertTypingImport
insertThirdPartyImports [ ("nirum.validate", ["validate_union_type"])
, ("nirum.constructs", ["name_dict_type"])
]
Expand Down Expand Up @@ -401,11 +402,11 @@ compileTypeExpression Source { sourceModule = boundModule } (TypeIdentifier i) =
compileTypeExpression source (MapModifier k v) = do
kExpr <- compileTypeExpression source k
vExpr <- compileTypeExpression source v
insertTypingImport
insertStandardImport "typing"
return [qq|typing.Mapping[$kExpr, $vExpr]|]
compileTypeExpression source modifier = do
expr <- compileTypeExpression source typeExpr
insertTypingImport
insertStandardImport "typing"
return [qq|typing.$className[$expr]|]
where
typeExpr :: TypeExpression
Expand All @@ -431,7 +432,6 @@ compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = UnboxedType itype } = do
let className = toClassName' typename'
itypeExpr <- compileTypeExpression src itype
insertTypingImport
insertThirdPartyImports [ ("nirum.validate", ["validate_boxed_type"])
, ("nirum.serialize", ["serialize_boxed_type"])
, ("nirum.deserialize", ["deserialize_boxed_type"])
Expand Down Expand Up @@ -524,7 +524,7 @@ compileTypeDeclaration src TypeDeclaration { typename = typename'
(map fieldName $ toList fields)
",\n "
hashText = toIndentedCodes (\ n -> [qq|self.{n}|]) fieldNames ", "
insertTypingImport
importTypingForPython3
insertThirdPartyImports [ ("nirum.validate", ["validate_record_type"])
, ("nirum.serialize", ["serialize_record_type"])
, ("nirum.deserialize", ["deserialize_record_type"])
Expand Down Expand Up @@ -586,7 +586,7 @@ compileTypeDeclaration src TypeDeclaration { typename = typename'
fieldCodes' = T.intercalate "\n\n" fieldCodes
enumMembers = toIndentedCodes
(\ (t, b) -> [qq|$t = '{b}'|]) enumMembers' "\n "
insertTypingImport
importTypingForPython3
insertEnumImport
insertThirdPartyImports [ ("nirum.serialize", ["serialize_union_type"])
, ("nirum.deserialize", ["deserialize_union_type"])
Expand Down
17 changes: 5 additions & 12 deletions test/Nirum/Targets/PythonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Data.Maybe (fromJust)

import qualified Data.Map.Strict as M
import qualified Data.SemVer as SV
import Data.Set (Set, union)
import System.FilePath ((</>))
import Test.Hspec.Meta
import Text.Email.Validate (emailAddress)
Expand Down Expand Up @@ -45,7 +44,6 @@ import qualified Nirum.Package.ModuleSet as MS
import Nirum.PackageSpec (createPackage)
import qualified Nirum.Targets.Python as PY
import Nirum.Targets.Python ( Source (Source)
, Code
, CodeGen
, CodeGenContext ( localImports
, standardImports
Expand Down Expand Up @@ -121,13 +119,8 @@ makeDummySource' pathPrefix m =
makeDummySource :: Module -> Source
makeDummySource = makeDummySource' []

versions :: [(PythonVersion, Set Code)]
versions = [ (Python2, [])
, (Python3, ["typing"])
]

spec :: Spec
spec = parallel $ forM_ versions $ \ (ver, typing) -> do
spec = parallel $ forM_ ([Python2, Python3] :: [PythonVersion]) $ \ ver -> do
let empty' = PY.empty ver
-- run' :: CodeGen a -> (Either CompileError a, CodeGenContext)
run' c = runCodeGen c empty'
Expand Down Expand Up @@ -212,25 +205,25 @@ spec = parallel $ forM_ versions $ \ (ver, typing) -> do
specify "OptionModifier" $ do
let (c', ctx') = run' $
compileTypeExpression s (OptionModifier "int32")
standardImports ctx' `shouldBe` typing
standardImports ctx' `shouldBe` ["typing"]
localImports ctx' `shouldBe` []
c' `shouldBe` Right "typing.Optional[int]"
specify "SetModifier" $ do
let (c'', ctx'') = run' $
compileTypeExpression s (SetModifier "int32")
standardImports ctx'' `shouldBe` typing
standardImports ctx'' `shouldBe` ["typing"]
localImports ctx'' `shouldBe` []
c'' `shouldBe` Right "typing.AbstractSet[int]"
specify "ListModifier" $ do
let (c''', ctx''') = run' $
compileTypeExpression s (ListModifier "int32")
standardImports ctx''' `shouldBe` typing
standardImports ctx''' `shouldBe` ["typing"]
localImports ctx''' `shouldBe` []
c''' `shouldBe` Right "typing.Sequence[int]"
specify "MapModifier" $ do
let (c'''', ctx'''') = run' $
compileTypeExpression s (MapModifier "uuid" "int32")
standardImports ctx'''' `shouldBe` union ["uuid"] typing
standardImports ctx'''' `shouldBe` ["typing", "uuid"]
localImports ctx'''' `shouldBe` []
c'''' `shouldBe` Right "typing.Mapping[uuid.UUID, int]"

Expand Down
5 changes: 5 additions & 0 deletions test/nirum_fixture/fixture/foo.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ record line (
bigint length,
);

record import-typing (
// see also: https://github.com/spoqa/nirum/issues/93
bigint? an-optional-field,
);

union mixed-name = western-name ( text first-name
, text middle-name
, text last-name
Expand Down

0 comments on commit 3f66839

Please sign in to comment.