Pylint syntax error not suppressing despite the `disable` comment

66 Views Asked by At

I'm writing scripts for Ansys Mechanical, which are written in IronPython 2.7.0.40. In my script, I need to assign a property to ContactTimeStepControls.None. Note that the None here is NOT the None object.[1]

My issue is that the Pylint extension in VS Code is flagging the use of None here as a syntax error (probably because it thinks it IS the None object) and refusing to evaluate the rest of the code. Here are there error messages it gives:

SyntaxError: invalid syntax (file:///[PathToMyScript], line 264) compile [Ln 264, Col 80]

Parsing failed: 'invalid syntax (, line 264)' Pylint(E0001:syntax-error) [Ln 264, Col 81]

I've tried disabling that error message the following two ways:

# pylint: disable=syntax-error
Cxn.TimeStepControls = ContactTimeStepControls.None

and

# pylint: disable=E0001
Cxn.TimeStepControls = ContactTimeStepControls.None

Both fail to suppress the error.

Here's a screenshot to demonstrate:

Example of Pylint Error

I therefore have three questions:

  1. Am I doing something wrong with the Pylint suppression?
  2. Is there some means of structuring the code to avoid the error (note that I can't change the members of ContactTimeStepControls)?
  3. Is there some other means of suppressing the Pylint errors on this line?

[1] Output from dir(ContactTimeStepControls) is this (see bolded item):

['AutomaticBisection', 'CompareTo', 'Equals', 'Format', 'GetHashCode', 'GetName', 'GetNames', 'GetType', 'GetTypeCode', 'GetUnderlyingType', 'GetValues', 'HasFlag', 'IsDefined', 'MemberwiseClone', 'None', 'Parse', 'PredictForImpact', 'ReferenceEquals', 'ToBoolean', 'ToByte', 'ToChar', 'ToDateTime', 'ToDecimal', 'ToDouble', 'ToInt16', 'ToInt32', 'ToInt64', 'ToObject', 'ToSByte', 'ToSingle', 'ToString', 'ToType', 'ToUInt16', 'ToUInt32', 'ToUInt64', 'TryParse', 'UseImpactConstraints', '__and__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__invert__', '__le__', '__lt__', '__ne__', '__new__', '__nonzero__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__xor__', 'value__']

Edit:

I just discovered another place where this is likely to occur that will probably be experienced more commonly by people. The DialogResult property (link) of the System.Windows.Forms module from Microsoft's .NET API has a None property, which would produce the same effect.

3

There are 3 best solutions below

0
On BEST ANSWER

Instead of using eval, you can parse the enum from a string

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import DialogResult

result = DialogResult.Parse(DialogResult, "None")

or in Mechanical scripting (tested in 2024 R1)

Cxn.TimeStepControls = ContactTimeStepControls.Parse(ContactTimeStepControls, "None")

Once again not ideal, but at least it avoids the Pylint errors.

5
On

Syntax errors cannot be disabled, they indicate that pylint is not compatible with Iron python and cannot parse your file. I'm on mobile so I can't link the Iron python issue from the github repo but there's one.

1
On

I found a workaround using eval():

Before:

Code with Syntax Error

After:

Code without Syntax Error

Obviously, the use of eval() makes this less than ideal. But the risk is low since the text is not being generated dynamically, and the benefit of being able to have Pylint evaluate the entire code greatly outweighs the downside of needing to use eval().