Skip to content

Create all of the RangeSets statically instead of using globals() #3468

Open
@deanm0000

Description

@deanm0000

Summary

I noticed that I can't import, for example NonNegativeReals or Binary, without the type checker giving me the squiggly red line of badness. I dug into it and found that these are dynamically loaded into globals and they're never actually declared.

pyomo/pyomo/core/base/set.py

Lines 4548 to 4555 in cd778c5

DeclareGlobalSet(
RangeSet(
name='PositiveReals',
doc='A global Pyomo Set admitting any real value in (0, +inf]',
ranges=(NumericRange(0, None, 0, (False, True)),),
),
globals(),
)

Rationale

type checkers can't deal with dynamically assigned objects.

Description

I'm not sure if it's necessary to make each of them its own class or if it'd be enough to just have them declared as constants in set_types.py. Something like

from pyomo.core.base.range import NumericRange
from pyomo.core.base.set import RangeSet
from typing import cast

NonNegativeReals=     cast(RangeSet,RangeSet(
        name='NonNegativeReals',
        doc='A global Pyomo Set admitting any real value in [0, +inf]',
        ranges=(NumericRange(0, None, 0),),
    )
)

without that explicit cast it wants to be RangeSet | AbstractFiniteScalarRangeSet | AbstractInfiniteScalarRangeSet

Ideally RangeSet would have overloads so that it only returns one thing but maybe that can be the next typing improvement.

Additional information

The above could even be wrapped in an if typing block since I don't really understand the implications of the docstring to DeclareGlobalSet

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions