Tirasz / transpy

(Thesis) Transforming conditions into structural patterns with Python AST
1 stars 2 forks source link

Faulty flattening logic #10

Open Tirasz opened 2 years ago

Tirasz commented 2 years ago

This:

if isinstance(channel, (Thread, TextChannel)) and guild is not None:
    member = guild.get_member(user_id)
    if member is None:
        member_data = data.get('member')
        if member_data:
            member = Member(data=member_data, state=self, guild=guild)

Got turned into this:

match channel:
    case Thread() | TextChannel() if guild is not None and member is None:
        member = guild.get_member(user_id)
        member_data = data.get('member')
        if member_data:
            member = Member(data=member_data, state=self, guild=guild)

Im not 100% sure, but i think this is because the nested if-node doesnt have an "else:" block, thus there is no case created for when only the main if-node's test is true, which is a pretty big oversight. Possible fixes:

Tirasz commented 2 years ago

What is happening in a simpler example:

if A == 2 and C:
    pre_nest()
    if B == 3:
        nested()

if A == 2 and C: 
    pre_nest()
    if B == 3:
        nested()
    else:
        nested_else()

Gets turned into:

match A:
    case 2 if C and B == 3:
        pre_nest()
        nested()

match A:
    case 2 if C and B == 3:
        pre_nest()
        nested()
    case 2:
        pre_nest()
        nested_else()

Many things are wrong with this but im too tired

Tirasz commented 2 years ago

image