pxt icon indicating copy to clipboard operation
pxt copied to clipboard

Issue with class constructors in python.

Open FegrusB opened this issue 4 years ago • 6 comments

I am having an issue when declaring a class in python. I am getting an error 'Type annotation cannot appear on a constructor declaration' I have not used any type annotation. Another user has identified this as an error with converting the python to typescript

class cord: def init (self, xCord, yCord): self.x = xCord self.y = yCord

Type annotation cannot appear on a constructor declaration.

FegrusB avatar Sep 14 '21 15:09 FegrusB

I'm still encountering this almost a year later. Is this issue still not addressed? Without this fix there is no way to use or teach class construction in Minecraft EE.

enauman avatar Jun 05 '22 15:06 enauman

@enauman @FegrusB This code works in beta

class cord:
    def __init__(self, xCord, yCord):
        self.x = xCord
        self.y = yCord

xy = cord(1,2)

def on_forever():
    basic.show_number(xy.x)
    basic.show_number(xy.y)

basic.forever(on_forever)

martinwork avatar Jun 05 '22 17:06 martinwork

Is there a beta for Minecraft EE? That's where I'm encountering the issue.

enauman avatar Jun 05 '22 17:06 enauman

I found the beta (click the gear, click Experimental), though it's not integrated into the game very well. Even so, I get the same error 'Type annotation cannot appear on a constructor declaration'

enauman avatar Jun 05 '22 19:06 enauman

Experiencing the same also when trying classes in Python on arcade.makecode.com; 'Type annotation cannot appear on a constructor declaration'

Issues: MAIN:
1. Getting the appended type in constructors for both main class and enumerator class in the "shadow" .ts file which is automatically made parallell to the .py file I am working on.
A little outside this scope, but worth mentioning: 2. The Python enumeration class (class Letter2(Enum):) has to be initialized to be referenced in the other class that uses it, and then a whole lot of strange code is generated in the .ts shadow file: Py_enumeration_class_ts.txt I am unable to import and use the enumeration class to make it accessible in one Python file from another "module" file, like was possible for the function below. UPDATE: I am wondering it this merge in fact is the reason for the auto generated code for my enumeration class that does not have a constructor?: https://github.com/microsoft/pxt/pull/8883 UPDATE END 3. While not officially supported, according to this page, I am glad it is still possible to import functions from Python module files, via from customp import functionName, however, there is a strange bug where in Python it is callable without the module file name prefix, while JavaScript makes a namespace with the module name and requires the module prefix namespace.function, forcing either an error in the .py or .ts file. A workaround was to append as anotherName in the import statement in the the .py file, and in a separate .ts file make a similar function reference alias/variable let anotherName = namespace.function, making it callable by anotherName in both main.py and main.ts...

Works a little better on the beta where everything including issue 1 compiles, in the main production webeditor, only 2 and 3. Import to make class accessible from another file in Python, ref. point 2 above, does not work in beta either.

Hope you can have a look at this soon, and nice to see that the Python part is getting closer to the functionality of the JavaScript!

Vegz78 avatar Jun 24 '22 01:06 Vegz78

I'm still encountering this almost a year later. Is this issue still not addressed? Without this fix there is no way to use or teach class construction in Minecraft EE.

@enauman: Seems like there has been some recent activity regarding classes in Python lately; https://github.com/microsoft/pxt/pull/8879

Unsure about how it affects this specific issue, though.

Vegz78 avatar Jul 03 '22 21:07 Vegz78