Hi
As we all know python is quite a flexible coding language.
You can choose to either specify your types or not.
My problem is that because ‘iron python’ (the platform in which our python code runs in) does not support types - I am forced to not declare types in any of my functions.
By not declaring the variable type, its type is ‘Any’ by default, which leads to no auto completion, which sucks so bad.
In this simple example, upon writing the function - the IDE wont suggest me any of the Element class properties or methods, simply because it doesn’t know that element is necessarily of type Element:
Not sure what IDE you are using but maybe this is what you need?
no autocomplete for pyrevit, but you will get for Autodesk.Revit.DB
Do you know C# already? You can always use VS or VS Code and skip the python entirely. Either compile a DLL and invoke with a pushbutton, or create a script.cs and just copy the external command into that file in the button folder.
Thanks but this is not what I meant.
I think that what I’m referring is called ‘Type Hinting’, specifically for custom functions that I wrote.
Going back to the simple example of the original post, when I write a function and declare which variables the function takes, I cannot declare the variable types and therefore I will not get suggestions from my IDE for that variable within the function’s scope, for example Name, Id upon typing element.
This is because the IDE does not know that the variable is necessarily of type Element.
Or in other words - is there a workaround the fact that IronPython does not support python types?
So I guess Ill learn how to use C#
I did notice that unlike pyRevit, with C# I have to rebuild my code and restart Revir for every change I make to the code… well this is annoying.
Replace script.py with script.cs. But i was assuming that you already knew c# based on the thread name comparing Python to it. The majority of pyrevit users likely don’t know c# and get by fine using Code or pyCharm with the stubs for completion.
late to the party here but I have an actual solution to the missing typehinting
Pycharm is pretty decent at it. It’s pretty good at detecting the types used in functions and will do its best to propagate those types. (provided you’ve setup your environment)
Now… It usually knows which type you’re working with and even gives you the right intellisense.
BUT it will struggle with some things sometimes, but then you can manually instruct the types using the comment style typehint, which pycharm understands and actually uses.
# to force pycharm to use the types you specify
def add(a,b):
# type: (int, int) -> int
return a + b
# you can also do this inline
my_var = get_my_int() # type: int
What’s nice about this is that it’s hidden in a comment, so it won’t break your python syntax.
notice that even the web code viewer highlights my comment style typehints syntax.
I use this all the time whenever I’m absolutely sure of the type but pycharm fails to figure it out.
Ofcourse this is not the same as a strongly typed language, but with enough typehints strewn about you can catch a lot of errors and oversights.
Another option you have (again in pycharm) is to use docstrings, reStructured (and I think Googler) styled docstrings are also parsed and used for typehinting.