nebius/SWE-bench-extra huggingface dataset:
Repository: PyCQA/pyflakes

Issue:
"undefined name in __all__" has non-deterministic ordering
Found this while putting together a tool to look for regressions in the latest release and noticed that this produces things out-of-order!

```console
$ cat t.py 
__all__ = ('a', 'b', 'c', 'd')
$ python3 -m pyflakes t.py
t.py:1:1 undefined name 'a' in __all__
t.py:1:1 undefined name 'd' in __all__
t.py:1:1 undefined name 'b' in __all__
t.py:1:1 undefined name 'c' in __all__
$ python3 -m pyflakes t.py
t.py:1:1 undefined name 'c' in __all__
t.py:1:1 undefined name 'd' in __all__
t.py:1:1 undefined name 'b' in __all__
t.py:1:1 undefined name 'a' in __all__
```

This is due to an intermediate set in the code for that check: https://github.com/PyCQA/pyflakes/blob/0e4194b238ffd006441f1a33373062d9c7272d0e/pyflakes/checker.py#L993-L997

Comments:

Patch:
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 0d65506..38f8f73 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -992,7 +992,10 @@ class Checker(object):
 
             if all_binding:
                 all_names = set(all_binding.names)
-                undefined = all_names.difference(scope)
+                undefined = [
+                    name for name in all_binding.names
+                    if name not in scope
+                ]
             else:
                 all_names = undefined = []
 
@@ -1680,6 +1683,19 @@ class Checker(object):
             with self._enter_annotation(AnnotationState.STRING):
                 self.handleNode(node.args[0], node)
 
+        elif _is_typing(node.func, 'TypeVar', self.scopeStack):
+            # TypeVar("T", "int", "str")
+            for arg in node.args[1:]:
+                if isinstance(arg, ast.Str):
+                    with self._enter_annotation():
+                        self.handleNode(arg, node)
+
+            # TypeVar("T", bound="str")
+            for keyword in node.keywords:
+                if keyword.arg == 'bound' and isinstance(keyword.value, ast.Str):
+                    with self._enter_annotation():
+                        self.handleNode(keyword.value, node)
+
         self.handleChildren(node)
 
     def _handle_percent_format(self, node):


Test patch:
diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py
index d5a3e08..409238b 100644
--- a/pyflakes/test/test_type_annotations.py
+++ b/pyflakes/test/test_type_annotations.py
@@ -524,6 +524,21 @@ class TestTypeAnnotations(TestCase):
         maybe_int = tsac('Maybe[int]', 42)
         """)
 
+    def test_quoted_TypeVar_constraints(self):
+        self.flakes("""
+        from typing import TypeVar, Optional
+
+        T = TypeVar('T', 'str', 'Optional[int]', bytes)
+        """)
+
+    def test_quoted_TypeVar_bound(self):
+        self.flakes("""
+        from typing import TypeVar, Optional, List
+
+        T = TypeVar('T', bound='Optional[int]')
+        S = TypeVar('S', int, bound='List[int]')
+        """)
+
     @skipIf(version_info < (3,), 'new in Python 3')
     def test_literal_type_typing(self):
         self.flakes("""
Repository: PyCQA/pyflakes

Issue:
Odd edge-case regression in type annotated `__all__`
I'm not quite sure how to approach fixing this -- it regressed as part of #535 -- CC @srittau 

Here's a simplified example, this is boiled down from numpy's `numpy/typing/__init__.py`

```python
from typing import TYPE_CHECKING, List

from y import z

if not TYPE_CHECKING:
    __all__ = ("z",)
else:
    __all__: List[str]
```

After #535, the type annotation clobbers the `__all__` "`ExportBinding`" which causes it to no longer mark the imports as used

I *think* we can make `Annotated` a `setdefault` on the latest namespace to prevent this?

Comments:

Patch:
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 38f8f73..215bd31 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -1140,7 +1140,10 @@ class Checker(object):
             # then assume the rebound name is used as a global or within a loop
             value.used = self.scope[value.name].used
 
-        self.scope[value.name] = value
+        # don't treat annotations as assignments if there is an existing value
+        # in scope
+        if value.name not in self.scope or not isinstance(value, Annotation):
+            self.scope[value.name] = value
 
     def _unknown_handler(self, node):
         # this environment variable configures whether to error on unknown


Test patch:
diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py
index 409238b..835d8d9 100644
--- a/pyflakes/test/test_type_annotations.py
+++ b/pyflakes/test/test_type_annotations.py
@@ -335,6 +335,19 @@ class TestTypeAnnotations(TestCase):
         def g(t: 'T'): pass
         ''')
 
+    @skipIf(version_info < (3, 6), 'new in Python 3.6')
+    def test_type_annotation_clobbers_all(self):
+        self.flakes('''\
+        from typing import TYPE_CHECKING, List
+
+        from y import z
+
+        if not TYPE_CHECKING:
+            __all__ = ("z",)
+        else:
+            __all__: List[str]
+        ''')
+
     def test_typeCommentsMarkImportsAsUsed(self):
         self.flakes("""
         from mod import A, B, C, D, E, F, G
Repository: PyCQA/pyflakes

Issue:
annotated variable hides "undefined name"
this works:

```console
$ pyflakes /dev/stdin <<< 'x = x'
/dev/stdin:1:5: undefined name 'x'
$
```

this does not:

```console
$ pyflakes /dev/stdin <<< 'x: int = x'
$
```

Comments:

Patch:
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index ae15621..56fc3ca 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -2265,7 +2265,6 @@ class Checker:
             self.scope[node.name] = prev_definition
 
     def ANNASSIGN(self, node):
-        self.handleNode(node.target, node)
         self.handleAnnotation(node.annotation, node)
         # If the assignment has value, handle the *value* now.
         if node.value:
@@ -2274,6 +2273,7 @@ class Checker:
                 self.handleAnnotation(node.value, node)
             else:
                 self.handleNode(node.value, node)
+        self.handleNode(node.target, node)
 
     def COMPARE(self, node):
         left = node.left


Test patch:
diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py
index 3775cd3..d881205 100644
--- a/pyflakes/test/test_type_annotations.py
+++ b/pyflakes/test/test_type_annotations.py
@@ -298,6 +298,11 @@ class TestTypeAnnotations(TestCase):
         a: 'a: "A"'
         ''', m.ForwardAnnotationSyntaxError)
 
+    def test_variable_annotation_references_self_name_undefined(self):
+        self.flakes("""
+        x: int = x
+        """, m.UndefinedName)
+
     def test_TypeAlias_annotations(self):
         self.flakes("""
         from typing_extensions import TypeAlias
Repository: PyCQA/pyflakes

Issue:
should produce error for attribute hidden by a method
# How to reproduce

```
$ flake8 --version
6.0.0 (mccabe: 0.7.0, pycodestyle: 2.10.0, pyflakes: 3.0.1) CPython 3.11.1 on Linux
$ flake8 ok.py
ok.py:5:5: F811 redefinition of unused 'bar' from line 2
$ flake8 fail.py
```

## `ok.py`

```python
class Foo:
    def bar(self):
        return 0

    def bar(self):
        return 1
```

## `fail.py`

```python
class Foo:
    bar = 0

    def bar(self):
        return 1
```

# Reasons

I do understand that redefinition of variables is a useful pattern, for example [`structlog` Bound Loggers](https://www.structlog.org/en/stable/bound-loggers.html) uses it extensively.

```python
log = structlog.get_logger()
log = log.bind(user=user)
log = log.bind(time=now())
log.info('hello world')
```

## Problems

I spent some time debugging a problem in our code base related to [Factory Boy Lazy Attributes](https://factoryboy.readthedocs.io/en/stable/#lazy-attributes).

```python
class UserFactory(Factory):
    class Meta:
        model = User

    name = 'joe'
    email = LazyAttribute(lambda user: f'{user.name}@example.com')

    # A 1000 lines later

    @post_generation
    def email(self):
        return 'fake@stub.io'
```

## Proposal

Deny attribute redefinition inside class body.

Thoughts?

Comments:

Patch:
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index e927715..4d778a8 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -263,6 +263,11 @@ class Definition(Binding):
     """
     A binding that defines a function or a class.
     """
+    def redefines(self, other):
+        return (
+            super().redefines(other) or
+            (isinstance(other, Assignment) and self.name == other.name)
+        )
 
 
 class Builtin(Definition):


Test patch:
diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py
index 42e99ae..aebdcea 100644
--- a/pyflakes/test/test_other.py
+++ b/pyflakes/test/test_other.py
@@ -118,6 +118,12 @@ class Test(TestCase):
         def a(): pass
         ''', m.RedefinedWhileUnused)
 
+    def test_redefined_function_shadows_variable(self):
+        self.flakes('''
+        x = 1
+        def x(): pass
+        ''', m.RedefinedWhileUnused)
+
     def test_redefinedUnderscoreFunction(self):
         """
         Test that shadowing a function definition named with underscore doesn't
Repository: PyPSA/PyPSA

Issue:
import_from_netcdf fails when importing from an XArray instance
## Checklist

- [X] I am using the current [`master`](https://github.com/pypsa/pypsa/tree/master) branch or the latest [release](https://github.com/pypsa/pypsa/releases).

## Describe the Bug

The [docs](https://pypsa.readthedocs.io/en/latest/api_reference.html#pypsa.Network.import_from_netcdf) state that `pypsa.Network.import_from_netcdf` should accept an xarray instance:

> import_from_netcdf(path, skip_time=False)
>
> Import network data from netCDF file or xarray Dataset at path.
> - path (string|xr.Dataset) – Path to netCDF dataset or instance of xarray Dataset. The string could be a URL.


However, a TypeError is raised if an XArray instance is passed. Possibly related: #381

## Minimal example:

```python
n1 = pypsa.Network()
xarr = n1.export_to_netcdf()
n2 = pypsa.Network()
n2.import_from_netcdf(xarr)
```

Gives error:

```
TypeError: expected str, bytes or os.PathLike object, not Dataset
```


Comments:

Patch:
diff --git a/pypsa/io.py b/pypsa/io.py
index 1fbb37d5..42fe4a65 100644
--- a/pypsa/io.py
+++ b/pypsa/io.py
@@ -653,7 +653,7 @@ def import_from_netcdf(network, path, skip_time=False):
     """
     assert has_xarray, "xarray must be installed for netCDF support."
 
-    basename = Path(path).name
+    basename = "" if isinstance(path, xr.Dataset) else Path(path).name
     with ImporterNetCDF(path=path) as importer:
         _import_from_importer(network, importer, basename=basename, skip_time=skip_time)
 


Test patch:
diff --git a/test/test_bugs.py b/test/test_bugs.py
index ddb1561a..e517457b 100644
--- a/test/test_bugs.py
+++ b/test/test_bugs.py
@@ -93,3 +93,14 @@ def test_515():
     n.lopf(pyomo=False)
 
     assert n.objective == 10
+
+
+def test_779():
+    """
+    Importing from xarray dataset.
+    """
+    n1 = pypsa.Network()
+    n1.add("Bus", "bus")
+    xarr = n1.export_to_netcdf()
+    n2 = pypsa.Network()
+    n2.import_from_netcdf(xarr)