You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are cases where mypy spits out an attr-defined error when accessing bar.foo; these can be ignore per-use with a # type: ignore[attr-defined], however, there doesn't currently seem to be a way to tell mypy that bar does indeed define foo.
In this case, it'd be nice if there was a way to tell mypy that bar does define foo, so that bar.foo can be used without spamming # type: ignore[attr-defined] comments everywhere.
How this is done I don't have an opinion about, but for example, it could be something like: # type: bar.foo: int.
This could allow type hinting for attributes added after a class definition. One example would be for users who enjoy using a TRACE log level with the logging module. That is, if a user adds a TRACE logging level to the logging module such as shown here: https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility
In this instance, this would allow use of logging.trace and logging.TRACE without requiring #type: ignore[attr-defined] used at every occurrence.
In general, this handles the case where you have something like:
classA:
passA.B="foo"# Some comment to promise mypy that A has an attr named B defined
Additionally, this can allow users to better handle when 3rd party libraries do not play nice with mypy. For example:
Feature
There are cases where mypy spits out an
attr-definederror when accessingbar.foo; these can be ignore per-use with a# type: ignore[attr-defined], however, there doesn't currently seem to be a way to tell mypy thatbardoes indeed definefoo.In this case, it'd be nice if there was a way to tell mypy that
bardoes definefoo, so thatbar.foocan be used without spamming# type: ignore[attr-defined]comments everywhere.How this is done I don't have an opinion about, but for example, it could be something like:
# type: bar.foo: int.Pitch
There are multiple useful cases of this:
Although this is not the primary motivation, it would cover the case brought up here: Support hints for dynamically set attributes #15085
This could allow type hinting for attributes added after a class definition. One example would be for users who enjoy using a
TRACElog level with the logging module. That is, if a user adds aTRACElogging level to theloggingmodule such as shown here: https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facilityIn this instance, this would allow use of
logging.traceandlogging.TRACEwithout requiring#type: ignore[attr-defined]used at every occurrence.In general, this handles the case where you have something like:
mypy. For example:This gives an
attr-definedwarning, although this import statement works just fine.