Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
deanm0000 opened this issue Feb 5, 2025 · 0 comments
Open

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

deanm0000 opened this issue Feb 5, 2025 · 0 comments

Comments

@deanm0000
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant