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

Add note for union types with missing attribute #17575

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Prabhat-Thapa45
Copy link

@Prabhat-Thapa45 Prabhat-Thapa45 commented Jul 24, 2024

This pull request fixes an issue #17036

If some attribute is not defined for a single union item (often None), this helps new users to narrow down by providing hint.
For example, the following code:

class A:
    def foo(self) -> int: pass

def f(x: Union[A, None]) -> int:
    return x.foo()

previously (on the master branch) generated this error message:

main.py:8: error: Item "B" of "A | B" has no attribute "x" [union-attr]

With this PR, the message remains, but we add a suggestion for how to fix the error:

main.py:8: error: Item "B" of "A | B" has no attribute "x"  [union-attr]
main.py:8: note: You can use 'if hasattr( a, 'x'):' to guard against missing attribute error

This comment has been minimized.

@Prabhat-Thapa45 Prabhat-Thapa45 changed the title Add note for union types Add note for union types with missing attribute Jul 24, 2024
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

check-jsonschema (https://github.com/python-jsonschema/check-jsonschema)
+ src/check_jsonschema/cli/param_types.py:90: note: You can use 'if hasattr( m, 'group'):' to guard against missing attribute error
+ src/check_jsonschema/cli/param_types.py:91: note: You can use 'if hasattr( m, 'group'):' to guard against missing attribute error

Tanjun (https://github.com/FasterSpeeding/Tanjun)
+ tanjun/dependencies/limiters.py:819: note: You can use 'if hasattr( resource, 'check'):' to guard against missing attribute error

cki-lib (https://gitlab.com/cki-project/cki-lib)
+ cki_lib/s3bucket.py:57: note: You can use 'if hasattr( boto3.DEFAULT_SESSION, 'get_credentials'):' to guard against missing attribute error
+ tests/test_cronjob.py:83: note: You can use 'if hasattr( runner, 'join'):' to guard against missing attribute error
+ tests/test_cronjob.py:109: note: You can use 'if hasattr( runner, 'join'):' to guard against missing attribute error
+ tests/test_cronjob.py:117: note: You can use 'if hasattr( runner, 'join'):' to guard against missing attribute error
+ tests/test_cronjob.py:127: note: You can use 'if hasattr( runner, 'join'):' to guard against missing attribute error

spack (https://github.com/spack/spack)
+ lib/spack/spack/util/spack_yaml.py:505: note: You can use 'if hasattr( <variable name>, '__next__'):' to guard against missing attribute error

operator (https://github.com/canonical/operator)
+ ops/lib/__init__.py:273: note: You can use 'if hasattr( self.spec.loader, 'exec_module'):' to guard against missing attribute error

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexing.py:912: note: You can use 'if hasattr( iloc, '_setitem_with_indexer'):' to guard against missing attribute error

aioredis (https://github.com/aio-libs/aioredis)
+ aioredis/connection.py:934: note: You can use 'if hasattr( <variable name>, 'split'):' to guard against missing attribute error

pip (https://github.com/pypa/pip)
+ src/pip/_internal/metadata/pkg_resources.py:182: note: You can use 'if hasattr( self._dist._provider, 'path'):' to guard against missing attribute error
+ src/pip/_internal/network/utils.py:65: note: You can use 'if hasattr( response.raw, 'stream'):' to guard against missing attribute error
+ src/pip/_internal/network/utils.py:95: note: You can use 'if hasattr( response.raw, 'read'):' to guard against missing attribute error
+ src/pip/_internal/network/auth.py:525: note: You can use 'if hasattr( resp.raw, 'release_conn'):' to guard against missing attribute error
+ src/pip/_internal/network/auth.py:548: note: You can use 'if hasattr( resp.request, 'url'):' to guard against missing attribute error
+ src/pip/_internal/network/session.py:408: note: You can use 'if hasattr( self.auth, 'index_urls'):' to guard against missing attribute error
+ src/pip/_internal/index/collector.py:88: note: You can use 'if hasattr( response.request, 'method'):' to guard against missing attribute error
+ src/pip/_internal/cli/index_command.py:128: note: You can use 'if hasattr( session.auth, 'prompting'):' to guard against missing attribute error
+ src/pip/_internal/cli/index_command.py:129: note: You can use 'if hasattr( session.auth, 'keyring_provider'):' to guard against missing attribute error

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/builders/html/__init__.py:448:16: note: You can use 'if hasattr( self._publisher.writer, 'parts'):' to guard against missing attribute error

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/internal/gitmetadata.py:77: note: You can use 'if hasattr( <variable name>, '__iter__'):' to guard against missing attribute error

ibis (https://github.com/ibis-project/ibis)
+ ibis/common/dispatch.py:34: note: You can use 'if hasattr( r, 'lstrip'):' to guard against missing attribute error
+ ibis/expr/datatypes/cast.py:20: note: You can use 'if hasattr( source, 'castable'):' to guard against missing attribute error
+ ibis/selectors.py:394: note: You can use 'if hasattr( self.funcs, 'items'):' to guard against missing attribute error
+ ibis/selectors.py:607: note: You can use 'if hasattr( key, '__iter__'):' to guard against missing attribute error
+ ibis/selectors.py:610: note: You can use 'if hasattr( key, 'start'):' to guard against missing attribute error
+ ibis/selectors.py:611: note: You can use 'if hasattr( key, 'stop'):' to guard against missing attribute error
+ ibis/selectors.py:612: note: You can use 'if hasattr( key, 'step'):' to guard against missing attribute error
+ ibis/expr/types/generic.py:1243: note: You can use 'if hasattr( data_mapper, 'convert_scalar'):' to guard against missing attribute error
+ ibis/expr/types/generic.py:1417: note: You can use 'if hasattr( data_mapper, 'convert_column'):' to guard against missing attribute error
+ ibis/expr/types/maps.py:498: note: You can use 'if hasattr( keys, 'keys'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:202: note: You can use 'if hasattr( data_mapper, 'convert_table'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:461: note: You can use 'if hasattr( schema, 'names'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:467: note: You can use 'if hasattr( schema, 'get'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:1116: note: You can use 'if hasattr( metrics, 'values'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:1117: note: You can use 'if hasattr( metrics, 'copy'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:1118: note: You can use 'if hasattr( having, '__iter__'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:3964: note: You can use 'if hasattr( match_result, 'groups'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:4379: note: You can use 'if hasattr( names, 'sort_values'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:4379: note: You can use 'if hasattr( names, 'columns'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:4385: note: You can use 'if hasattr( names, 'columns'):' to guard against missing attribute error
+ ibis/expr/types/relations.py:4387: note: You can use 'if hasattr( names, 'itertuples'):' to guard against missing attribute error
+ ibis/expr/types/json.py:230: note: You can use 'if hasattr( dtype, 'is_string'):' to guard against missing attribute error
+ ibis/expr/types/json.py:232: note: You can use 'if hasattr( dtype, 'is_boolean'):' to guard against missing attribute error
+ ibis/expr/types/json.py:234: note: You can use 'if hasattr( dtype, 'is_integer'):' to guard against missing attribute error
+ ibis/expr/types/json.py:237: note: You can use 'if hasattr( dtype, 'is_floating'):' to guard against missing attribute error
+ ibis/expr/types/json.py:240: note: You can use 'if hasattr( dtype, 'is_map'):' to guard against missing attribute error
+ ibis/expr/types/json.py:243: note: You can use 'if hasattr( dtype, 'is_array'):' to guard against missing attribute error
+ ibis/backends/sql/datatypes.py:1092: note: You can use 'if hasattr( scale, 'this'):' to guard against missing attribute error
+ ibis/backends/tests/base.py:108: note: You can use 'if hasattr( <variable name>, '__file__'):' to guard against missing attribute error
+ ibis/backends/sql/compilers/base.py:1124: note: You can use 'if hasattr( op, '__func_name__'):' to guard against missing attribute error
+ ibis/backends/sql/compilers/base.py:1125: note: You can use 'if hasattr( op, '__input_type__'):' to guard against missing attribute error
+ ibis/backends/sql/compilers/base.py:1131: note: You can use 'if hasattr( op, '__udf_namespace__'):' to guard against missing attribute error
+ ibis/examples/gen_registry.py:329: note: You can use 'if hasattr( <variable name>, 'get'):' to guard against missing attribute error
+ ibis/backends/sql/compilers/pyspark.py:315: note: You can use 'if hasattr( op, '__func__'):' to guard against missing attribute error
+ ibis/backends/sql/compilers/pyspark.py:316: note: You can use 'if hasattr( op, '__func_name__'):' to guard against missing attribute error
+ ibis/backends/druid/tests/conftest.py:87: note: You can use 'if hasattr( match, 'groupdict'):' to guard against missing attribute error
+ ibis/backends/trino/__init__.py:59: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/snowflake/__init__.py:759: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/snowflake/__init__.py:766: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/pyspark/__init__.py:404: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/postgres/__init__.py:800: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/oracle/__init__.py:222: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/mysql/__init__.py:278: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/mssql/__init__.py:308: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/duckdb/__init__.py:95: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/datafusion/__init__.py:224: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error
+ ibis/backends/datafusion/__init__.py:319: note: You can use 'if hasattr( catalog, 'database'):' to guard against missing attribute error
+ ibis/backends/datafusion/__init__.py:321: note: You can use 'if hasattr( catalog, 'database'):' to guard against missing attribute error
+ ibis/backends/datafusion/__init__.py:323: note: You can use 'if hasattr( database, 'table'):' to guard against missing attribute error
+ ibis/backends/clickhouse/__init__.py:267: note: You can use 'if hasattr( expr, 'op'):' to guard against missing attribute error
+ ibis/backends/clickhouse/__init__.py:466: note: You can use 'if hasattr( query, 'sql'):' to guard against missing attribute error

urllib3 (https://github.com/urllib3/urllib3)
+ test/with_dummyserver/test_proxy_poolmanager.py:52: note: You can use 'if hasattr( connection, 'proxy_is_verified'):' to guard against missing attribute error
+ test/with_dummyserver/test_proxy_poolmanager.py:53: note: You can use 'if hasattr( connection, 'is_verified'):' to guard against missing attribute error

spark (https://github.com/apache/spark)
+ python/pyspark/sql/pandas/conversion.py:404: note: You can use 'if hasattr( data, 'columns'):' to guard against missing attribute error

rich (https://github.com/Textualize/rich)
+ rich/console.py:92: note: You can use 'if hasattr( sys.__stdin__, 'fileno'):' to guard against missing attribute error
+ rich/console.py:96: note: You can use 'if hasattr( sys.__stdout__, 'fileno'):' to guard against missing attribute error
+ rich/console.py:100: note: You can use 'if hasattr( sys.__stderr__, 'fileno'):' to guard against missing attribute error

pytest (https://github.com/pytest-dev/pytest)
+ testing/_py/test_local.py:860: note: You can use 'if hasattr( x, 'check'):' to guard against missing attribute error
+ testing/_py/test_local.py:863: note: You can use 'if hasattr( x, 'dirpath'):' to guard against missing attribute error
+ testing/_py/test_local.py:914: note: You can use 'if hasattr( x, 'check'):' to guard against missing attribute error
+ testing/_py/test_local.py:916: note: You can use 'if hasattr( y, 'check'):' to guard against missing attribute error
+ testing/_py/test_local.py:925: note: You can use 'if hasattr( x, 'basename'):' to guard against missing attribute error
+ testing/_py/test_local.py:926: note: You can use 'if hasattr( x, 'dirpath'):' to guard against missing attribute error
+ testing/_py/test_local.py:931: note: You can use 'if hasattr( x, 'sysexec'):' to guard against missing attribute error
+ testing/_py/test_local.py:942: note: You can use 'if hasattr( x, 'sysexec'):' to guard against missing attribute error
+ testing/_py/test_local.py:1283: note: You can use 'if hasattr( cmd, 'new'):' to guard against missing attribute error
+ testing/_py/test_local.py:1285: note: You can use 'if hasattr( cmd, 'relto'):' to guard against missing attribute error
+ testing/_py/test_local.py:1286: note: You can use 'if hasattr( x, 'check'):' to guard against missing attribute error

dragonchain (https://github.com/dragonchain/dragonchain)
+ dragonchain/broadcast_processor/broadcast_functions.py:95:32: note: You can use 'if hasattr( <variable name>, 'group'):' to guard against missing attribute error
+ dragonchain/lib/authorization.py:315:19: note: You can use 'if hasattr( <variable name>, 'group'):' to guard against missing attribute error
+ dragonchain/lib/authorization.py:317:25: note: You can use 'if hasattr( <variable name>, 'group'):' to guard against missing attribute error
+ dragonchain/lib/authorization.py:340:31: note: You can use 'if hasattr( <variable name>, 'group'):' to guard against missing attribute error

discord.py (https://github.com/Rapptz/discord.py)
+ discord/mentions.py:126: note: You can use 'if hasattr( self, '__iter__'):' to guard against missing attribute error
+ discord/mentions.py:131: note: You can use 'if hasattr( self, '__iter__'):' to guard against missing attribute error
+ discord/invite.py:466: note: You can use 'if hasattr( guild, 'get_channel'):' to guard against missing attribute error
+ discord/poll.py:385: note: You can use 'if hasattr( question_data, 'get'):' to guard against missing attribute error
+ discord/guild.py:3220: note: You can use 'if hasattr( channel, 'id'):' to guard against missing attribute error
+ discord/app_commands/commands.py:234: note: You can use 'if hasattr( callback, 'pass_command_binding'):' to guard against missing attribute error
+ discord/app_commands/commands.py:682: note: You can use 'if hasattr( callback, '__self__'):' to guard against missing attribute error
+ discord/app_commands/commands.py:683: note: You can use 'if hasattr( callback, '__func__'):' to guard against missing attribute error
+ discord/app_commands/commands.py:2469: note: You can use 'if hasattr( func, '__discord_app_commands_checks__'):' to guard against missing attribute error
+ discord/app_commands/commands.py:2471: note: You can use 'if hasattr( func, '__discord_app_commands_checks__'):' to guard against missing attribute error
+ discord/ext/commands/core.py:430: note: You can use 'if hasattr( func, '__commands_checks__'):' to guard against missing attribute error
+ discord/ext/commands/core.py:438: note: You can use 'if hasattr( func, '__commands_cooldown__'):' to guard against missing attribute error
+ discord/ext/commands/core.py:451: note: You can use 'if hasattr( func, '__commands_max_concurrency__'):' to guard against missing attribute error
+ discord/ext/commands/core.py:468: note: You can use 'if hasattr( func, '__before_invoke__'):' to guard against missing attribute error
+ discord/ext/commands/core.py:476: note: You can use 'if hasattr( func, '__after_invoke__'):' to guard against missing attribute error
+ discord/ext/commands/hybrid.py:309: note: You can use 'if hasattr( wrapped.callback, '__signature__'):' to guard against missing attribute error
+ discord/ext/commands/hybrid.py:319: note: You can use 'if hasattr( wrapped.callback, '__signature__'):' to guard against missing attribute error

xarray (https://github.com/pydata/xarray)
+ xarray/plot/facetgrid.py:720: note: You can use 'if hasattr( self._hue_var, 'to_numpy'):' to guard against missing attribute error

core (https://github.com/home-assistant/core)
+ homeassistant/components/linear_garage_door/coordinator.py:56: note: You can use 'if hasattr( self, '__iter__'):' to guard against missing attribute error
+ homeassistant/components/file_upload/__init__.py:187: note: You can use 'if hasattr( fut, 'done'):' to guard against missing attribute error

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/id.py:558: note: You can use 'if hasattr( session, 'get'):' to guard against missing attribute error

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/utilities/dispatch.py:191: note: You can use 'if hasattr( registry, 'get'):' to guard against missing attribute error
+ src/prefect/utilities/collections.py:185: note: You can use 'if hasattr( types, '__iter__'):' to guard against missing attribute error
+ src/prefect/server/schemas/states.py:273: note: You can use 'if hasattr( self.name, 'lower'):' to guard against missing attribute error
+ src/prefect/utilities/templating.py:345: note: You can use 'if hasattr( client, 'read_variable_by_name'):' to guard against missing attribute error
+ src/prefect/utilities/templating.py:354: note: You can use 'if hasattr( client, 'read_variable_by_name'):' to guard against missing attribute error
+ src/prefect/utilities/importtools.py:166: note: You can use 'if hasattr( spec, 'loader'):' to guard against missing attribute error
+ src/prefect/utilities/importtools.py:166: note: You can use 'if hasattr( spec.loader, 'exec_module'):' to guard against missing attribute error
+ src/prefect/utilities/importtools.py:341: note: You can use 'if hasattr( real_spec, 'origin'):' to guard against missing attribute error
+ src/prefect/utilities/importtools.py:342: note: You can use 'if hasattr( real_spec, 'submodule_search_locations'):' to guard against missing attribute error
+ src/prefect/client/subscriptions.py:30: note: You can use 'if hasattr( base_url, 'replace'):' to guard against missing attribute error
+ src/prefect/client/schemas/objects.py:388: note: You can use 'if hasattr( self.name, 'lower'):' to guard against missing attribute error
+ src/prefect/_internal/concurrency/services.py:193: note: You can use 'if hasattr( self._done_event, 'is_set'):' to guard against missing attribute error
+ src/prefect/_internal/concurrency/services.py:198: note: You can use 'if hasattr( self._done_event, 'wait'):' to guard against missing attribute error
+ src/prefect/client/base.py:297: note: You can use 'if hasattr( response, 'status_code'):' to guard against missing attribute error
+ src/prefect/client/base.py:511: note: You can use 'if hasattr( response, 'status_code'):' to guard against missing attribute error
+ src/prefect/variables.py:72: note: You can use 'if hasattr( variable, 'value'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:80: note: You can use 'if hasattr( schema.block_type, 'slug'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:782: note: You can use 'if hasattr( client, 'read_block_document_by_name'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:1021: note: You can use 'if hasattr( client, 'create_block_document'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:1028: note: You can use 'if hasattr( client, 'read_block_document_by_name'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:1032: note: You can use 'if hasattr( client, 'update_block_document'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:1038: note: You can use 'if hasattr( client, 'read_block_document'):' to guard against missing attribute error
+ src/prefect/blocks/core.py:1084: note: You can use 'if hasattr( client, 'delete_block_document'):' to guard against missing attribute error
+ src/prefect/runner/storage.py:293: note: You can use 'if hasattr( <variable name>, 'get_block_placeholder'):' to guard against missing attribute error
+ src/prefect/client/orchestration.py:3205: note: You can use 'if hasattr( id_or_name, 'lower'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:415: note: You can use 'if hasattr( value, '__iter__'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:898: note: You can use 'if hasattr( image, 'reference'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:903: note: You can use 'if hasattr( image, 'build'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:907: note: You can use 'if hasattr( image, 'reference'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:919: note: You can use 'if hasattr( image, 'push'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:923: note: You can use 'if hasattr( image, 'reference'):' to guard against missing attribute error
+ src/prefect/deployments/runner.py:927: note: You can use 'if hasattr( image, 'reference'):' to guard against missing attribute error
+ src/prefect/utilities/urls.py:169: note: You can use 'if hasattr( obj, '_block_document_id'):' to guard against missing attribute error
+ src/prefect/utilities/urls.py:171: note: You can use 'if hasattr( obj, 'name'):' to guard against missing attribute error
+ src/prefect/utilities/urls.py:187: note: You can use 'if hasattr( url_format, 'format'):' to guard against missing attribute error
+ src/prefect/utilities/urls.py:191: note: You can use 'if hasattr( url_format, 'format'):' to guard against missing attribute error
+ src/prefect/server/orchestration/rules.py:263: note: You can use 'if hasattr( <variable name>, 'data'):' to guard against missing attribute error
+ src/prefect/server/orchestration/rules.py:288: note: You can use 'if hasattr( self.session, 'add'):' to guard against missing attribute error
+ src/prefect/server/orchestration/rules.py:291: note: You can use 'if hasattr( self.session, 'flush'):' to guard against missing attribute error
+ src/prefect/server/orchestration/rules.py:417: note: You can use 'if hasattr( <variable name>, 'data'):' to guard against missing attribute error
+ src/prefect/server/orchestration/rules.py:447: note: You can use 'if hasattr( self.session, 'add'):' to guard against missing attribute error
+ src/prefect/server/orchestration/rules.py:450: note: You can use 'if hasattr( self.session, 'flush'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:187: note: You can use 'if hasattr( full_child_block_document, 'data'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:195: note: You can use 'if hasattr( full_child_block_document, 'block_document_references'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:310: note: You can use 'if hasattr( sort, 'as_sql_sort'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:369: note: You can use 'if hasattr( sort, 'as_sql_sort'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:407: note: You can use 'if hasattr( block_document, 'block_schema_id'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:418: note: You can use 'if hasattr( block_document, 'block_schema_id'):' to guard against missing attribute error
+ src/prefect/server/models/block_documents.py:420: note: You can use 'if hasattr( block_document, 'block_schema'):' to guard against missing attribute error
+ src/prefect/server/orchestration/instrumentation_policies.py:38: note: You can use 'if hasattr( context.initial_state, 'timestamp'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:80: note: You can use 'if hasattr( context.proposed_state, 'type'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:93: note: You can use 'if hasattr( context.proposed_state, 'name'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:106: note: You can use 'if hasattr( context.proposed_state, 'is_running'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:108: note: You can use 'if hasattr( context.proposed_state, 'timestamp'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:121: note: You can use 'if hasattr( context.proposed_state, 'timestamp'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:141: note: You can use 'if hasattr( context.proposed_state, 'is_final'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:147: note: You can use 'if hasattr( context.proposed_state, 'is_final'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:150: note: You can use 'if hasattr( context.proposed_state, 'timestamp'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:166: note: You can use 'if hasattr( context.proposed_state, 'timestamp'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:180: note: You can use 'if hasattr( context.proposed_state, 'is_running'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:204: note: You can use 'if hasattr( proposed_state, 'is_running'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:204: note: You can use 'if hasattr( proposed_state, 'is_final'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:221: note: You can use 'if hasattr( context.proposed_state, 'is_running'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:240: note: You can use 'if hasattr( context.proposed_state, 'is_scheduled'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:242: note: You can use 'if hasattr( context.proposed_state, 'state_details'):' to guard against missing attribute error
+ src/prefect/server/orchestration/global_policy.py:245: note: You can use 'if hasattr( context.proposed_state, 'timestamp'):' to guard against missing attribute error

... (truncated 177 lines) ...

materialize (https://github.com/MaterializeInc/materialize)
+ misc/python/materialize/mzcompose/services/materialized.py:99: note: You can use 'if hasattr( system_parameter_defaults, 'update'):' to guard against missing attribute error
+ misc/python/materialize/mzcompose/services/materialized.py:105: note: You can use 'if hasattr( system_parameter_defaults, 'items'):' to guard against missing attribute error
+ misc/python/materialize/lint/lint.py:136: note: You can use 'if hasattr( thread.duration, 'total_seconds'):' to guard against missing attribute error
+ ci/mkpipeline.py:464: note: You can use 'if hasattr( step, 'get'):' to guard against missing attribute error
+ misc/python/materialize/parallel_workload/parallel_workload.py:356: note: You can use 'if hasattr( worker.exe, 'last_log'):' to guard against missing attribute error
+ misc/python/materialize/cli/cloudbench.py:163: note: You can use 'if hasattr( r, 'fieldnames'):' to guard against missing attribute error
+ misc/python/materialize/cli/cloudbench.py:164: note: You can use 'if hasattr( r, 'fieldnames'):' to guard against missing attribute error

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ web/gen/tflow_js.py:31: note: You can use 'if hasattr( tf_http.response, 'trailers'):' to guard against missing attribute error

kornia (https://github.com/kornia/kornia)
+ kornia/enhance/histogram.py:217: note: You can use 'if hasattr( image, 'device'):' to guard against missing attribute error
+ kornia/enhance/histogram.py:217: note: You can use 'if hasattr( image, 'dtype'):' to guard against missing attribute error
+ kornia/enhance/histogram.py:220: note: You can use 'if hasattr( image, 'unsqueeze'):' to guard against missing attribute error
+ kornia/enhance/histogram.py:241: note: You can use 'if hasattr( image, 'dim'):' to guard against missing attribute error
+ kornia/enhance/histogram.py:244: note: You can use 'if hasattr( image, 'dim'):' to guard against missing attribute error
+ kornia/enhance/histogram.py:249: note: You can use 'if hasattr( image, 'dim'):' to guard against missing attribute error
+ kornia/enhance/histogram.py:251: note: You can use 'if hasattr( image, 'dim'):' to guard against missing attribute error

werkzeug (https://github.com/pallets/werkzeug)
+ tests/test_wrappers.py:179: note: You can use 'if hasattr( a, 'type'):' to guard against missing attribute error
+ tests/test_wrappers.py:180: note: You can use 'if hasattr( a, 'username'):' to guard against missing attribute error
+ tests/test_wrappers.py:181: note: You can use 'if hasattr( a, 'password'):' to guard against missing attribute error
+ tests/test_wrappers.py:189: note: You can use 'if hasattr( a, 'type'):' to guard against missing attribute error
+ tests/test_wrappers.py:190: note: You can use 'if hasattr( a, 'username'):' to guard against missing attribute error
+ tests/test_wrappers.py:191: note: You can use 'if hasattr( a, 'password'):' to guard against missing attribute error
+ tests/test_wrappers.py:1078: note: You can use 'if hasattr( req.range, 'ranges'):' to guard against missing attribute error
+ tests/test_wrappers.py:1081: note: You can use 'if hasattr( req.range, 'make_content_range'):' to guard against missing attribute error
+ tests/test_test.py:166: note: You can use 'if hasattr( b.input_stream, 'getvalue'):' to guard against missing attribute error
+ tests/test_test.py:168: note: You can use 'if hasattr( b.input_stream, 'getvalue'):' to guard against missing attribute error
+ tests/test_test.py:306: note: You can use 'if hasattr( request.authorization, 'username'):' to guard against missing attribute error

... (truncated 149 lines) ...```

@Prabhat-Thapa45 Prabhat-Thapa45 marked this pull request as ready for review July 24, 2024 09:39
@JelleZijlstra
Copy link
Member

I don't think we should encourage hasattr() checks. This adds an attribute of type Any, not something we should encourage.

@Prabhat-Thapa45
Copy link
Author

#17036

@JelleZijlstra could you please explain by it adds attribute of type Any. As far as I understood it only checks if an object has a specific attribute. Am i missing something but I can change it to isinstance() one

@JelleZijlstra
Copy link
Member

https://mypy-play.net/?mypy=latest&python=3.12&gist=9bcc9b1ea9eb782372d7662fe53b2c5d

@hauntsaninja hauntsaninja added the pending Issues that may be closed label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending Issues that may be closed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants