-
Notifications
You must be signed in to change notification settings - Fork 301
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
[Python] Should obj ()
generate an empty dict ?
#3598
Comments
Sounds like a good idea! Alt least it should not be However, the problem with The problem with the above code is that in release mode the property will be inlined and we will use attribute access which will fail with Python. I need to look into the old logic to see if it still makes sense and what we can do with it (if any). |
Could you tell more about the use-case? Can you have special code for Python or is it similar code that needs to be used for Python and JS? PS: The .NET |
Candidates for
|
The usage that I am working on Thoth.Json (library agnostic version). This means that I am creating a core library which has some helpers contract that can then be used implemented by the different runtimes like JavaScript, Python, Newtonsoft, etc. For object, my current API implementation looks like this: let rec toJsonValue (helpers: IEncoderHelpers<'JsonValue>) (json: Json) =
match json with
| Json.Object values ->
let o = helpers.createEmptyObject ()
values
|> Seq.iter (fun (k, v) ->
helpers.setPropertyOnObject (o, k, toJsonValue helpers v)
)
o Where for JavaScript I have this implementation [<RequireQualifiedAccess>]
module Encode =
let helpers =
{ new IEncoderHelpers<obj> with
member _.createEmptyObject() = obj ()
member _.setPropertyOnObject(o: obj, key: string, value: obj) =
o?(key) <- value
} For newtonsoft: [<RequireQualifiedAccess>]
module Encode =
let helpers =
{ new IEncoderHelpers<JToken> with
member _.createEmptyObject() = JObject()
member _.setPropertyOnObject
(
o: JToken,
key: string,
value: JToken
)
=
o[key] <- value
} And for Python I do: [<RequireQualifiedAccess>]
module Encode =
let helpers =
{ new IEncoderHelpers<obj> with
member _.createEmptyObject() = emitPyExpr () "{}"
member _.setPropertyOnObject(o: obj, key: string, value: obj) =
o?(key) <- value
} For now for Python, I am using Note: The object created this way will then be pass to
This is indeed something I discovered yesterday. And I was wondering if in Python Also when reading about how to create an empty object in Python, some people said that |
I think you are right about |
Hum, in my case I don't think I can use type IEncoderHelpers<'JsonValue> =
abstract createEmptyObject: unit -> 'Object
abstract setPropertyOnObject: 'JsonValue * string * 'JsonValue -> unit
abstract encodeString: string -> 'JsonValue
let rec toJsonValue (helpers: IEncoderHelpers<'JsonValue>) (json: Json) : 'JsonValue =
// ... I tried to add a new I could probably make it works by using some |
The problem I have with the Fable AST is that it has a Set expr, but I don't know how I can tell if it should be subscript/index (e.g |
Is there an important difference between them? What if we always used subscript/index notation when using dynamic typing? I am really naive I know ^^ |
@ncave I'm unsure about the Fable AST if there is a way to tell if something should be subscripted or use attribute access for |
@dbrattli Usually indexers are compiled as method calls, but perhaps you mean something else. Can you paste a small F# example of both? |
@dbrattli Can this is be closed or do you still need to discuss some of the raised issues from the discussion? |
@MangelMaxime I don't think the issue is fixed yet. The code will now generate an empty dict for obj (), but setters and getters still do not work as expected. I'm planning a larger refactor to try and fix this issue, but haven't had the time yet. Perhaps in the holidays ahead. |
Description
Currently
let a = obj ()
generatesb : Any = None
but this cause problem if later user try to set a property on that object.This generates the error:
But changing the emit code to
{}
works:It generates
Related information
The text was updated successfully, but these errors were encountered: