Giter VIP home page Giter VIP logo

Comments (18)

vberlier avatar vberlier commented on July 21, 2024 1

Fixed in 6cb3b41. This was due to the fact that empty list tags don't have a concrete subtype. Now the subtype of empty list tags wil be inferred from the other list tags in the list.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024 1

Version 1.3.2 has the fix for compound keys that were interpreted as numbers so I'm closing the issue as I won't be fixing the list problem. I might be adding a "caveats" or "notes" section to the README later that will warn users that as opposed to the spec, nbtlib assumes lists to be deeply uniform.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024 1

My bad, this should be fixed in 1e7c90a.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

Closing the issue as the fix is now in release 1.3.0.

from nbtlib.

ch-yx avatar ch-yx commented on July 21, 2024

it seems like it is not fixed completely
image_156
lists of a list may contain different type of value.

image_157
(by the way,keys start with a number is allowed. )

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

Hmm I think the list problem is going to be a bit tricky to work with. List tags are supposed to have a uniform subtype, meaning that each list element must be of the same type:

>>> parse_nbt("[1,2,hello]")
Traceback (most recent call last):
...
nbtlib.literal.InvalidLiteral: Item 'hello' is not a Int tag at position 10

The thing is that lists containing different types of elements have themselves a different type:

>>> integers = parse_nbt('[1,2,3]')
>>> strings = parse_nbt('[a,b,c]')
>>> type(integers) is List[Int]
True
>>> type(strings) is List[String]
True
>>> List[Int] is not List[String]
True

This means that lists that contain different types of elements can't be placed in another list:

>>> boom = List([integers, strings])
Traceback (most recent call last):
...
nbtlib.tag.IncompatibleItemType: String('a') should be a Int tag

This is not a parsing problem, it has to do with the fact that nbtlib requires list subtypes to be deeply uniform. Changing this behavior will break nbtlib.schema so I'm afraid I'm not going to be able to fix it. Schemas rely on being able to deeply inspect the subtype of lists to cast everything to the appropriate type:

>> Thing = schema('Thing', {
...     'bigList': List[List[schema('ThingElement', {
...         'value': Int
...     })]]
... })
>>> thing = Thing({'bigList': [[{'value': 5}], [], [], [{'value': -1}, {'value': -2}]]})
>>> type(thing['bigList'])
<class 'nbtlib.tag.List[List[ThingElement]]'>

In think that this problem is kind of an edge-case, though. I don't think you would ever run into it, I'm pretty sure Minecraft doesn't use lists that contain lists of different subtypes. IMO the advantages of being able to define schemas outweigh the slight spec mismatch.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

However, I can do something about compound keys starting with a number.

from nbtlib.

ch-yx avatar ch-yx commented on July 21, 2024

I am 100% agree with you.
I guess that anyone who uses list this way must be mad.XD

I just try to find how reliable your amazing work is.

thanks a lot for fixing my issues these days though all of them are minor bug and not getting annoyed at my poor English.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

Haha no problem, thanks for taking the time to test everything so thoroughly! English isn't my native language either so don't worry about it ;)

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

Alright so I fixed a few issues with number and string parsing and in 94f9906 you can now use compound keys that would usually be parsed as numbers.

from nbtlib.

ch-yx avatar ch-yx commented on July 21, 2024

I come across some thing interesting:

while boom = List([integers, strings]) leads to a Error,
boom = List[List]([integers, strings]) works well.

Have you fixed it already?

from nbtlib.

ch-yx avatar ch-yx commented on July 21, 2024

i have not found anything wrong with the List[List] object it return yet.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

Oh that's interesting. I never really thought about it but the behavior actually makes perfect sense.

1. List[List]([integers, strings])

List tags can only contain instances of their respective subtype. If you're putting an object that's not an nbt tag into a list, nbtlib will attempt to cast the object to the list subtype (i.e.: subtype(obj)).

>>> integers = List[Int]([1, 2, 3])
>>> integers[0]
Int(1)

The reason why List[List]([integers, strings]) works is because integers and strings are already nbt tags so nbtlib simply checks that they're both instances of the combined list subtype.

>>> integers = List[Int]([1, 2, 3])
>>> strings = List[String](['a', 'b', 'c'])
>>> combined = List[List]([integers, strings])
>>> combined.subtype is List
True
>>> isinstance(integers, List)
True
>>> isinstance(strings, List)
True

2. List([integers, strings])

For convenience's sake, instantiating a list with the List class directly will infer the list subtype from the specified values.

>>> integers = List([1, 2, Int(3)])
>>> type(integers) is List[Int]
True
>>> integers
List[Int]([Int(1), Int(2), Int(3)])

Since List[List]([integers, strings]) works, this means that in the end, the only reason why List([integers, strings]) doesn't work is because the code that infers the subtype from the specified elements is currently a bit too strict.

3. Implications

In a previous comment, I said that nbtlib requires lists to be deeply uniform, but I didn't think about List[List], which is kind of embarrassing considering I wrote the code... πŸ˜… Anyway, I'm going to make sure that List[List] can be used without any problem.

Converting List[List] instances to and from the binary format already works.

>>> type(combined)
<class 'nbtlib.tag.List[List]'>
>>> b = BytesIO()
>>> combined.write(b)
>>> b.seek(0)
0
>>> List.parse(b) == combined
True

Snbt serialization is already working as well.

>>> print(combined)
[[1, 2, 3], ["a", "b", "c"]]

However, parsing non-deeply uniform list tags from snbt currently raises an exception.

>>> parse_nbt('[[1, 2, 3], ["a", "b", "c"]]')
Traceback (most recent call last):
...
nbtlib.literal.parser.InvalidLiteral: Item '"a"' is not a Int tag at position 27

This is because the parser uses the List class directly to create the list tag instance, and therefore relies on inference to determine the subtype. By adjusting the inference code and making it so that List([integers, strings]) works, the problem will resolve itself.

I'm reopening the issue. nbtlib does in fact support non-deeply uniform list tags through List[List], the problem is simply that the code for inferring the subtype of a list doesn't consider List as a possible subtype.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

Alright this should be fixed in 69f7c28.

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

The fix is now in release 1.4.5, closing the issue.

from nbtlib.

ch-yx avatar ch-yx commented on July 21, 2024

Sorry. =_=

I just found that

List([List[Int]([Int(1)]),List[List]([List[Int]([])])]) (or [[1], [[]]]οΌ‰
the outermost List still does not get its subtype properly..........

thus,parse_nbt('[[1], [[]]]') couldn`t works too.

from nbtlib.

ch-yx avatar ch-yx commented on July 21, 2024

please reeopeen this issue

from nbtlib.

vberlier avatar vberlier commented on July 21, 2024

And... it's live in v1.4.6.

from nbtlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.