nebius/SWE-bench-extra huggingface dataset:
Repository: protolambda/remerkleable

Issue:
Check if the field name is valid when initialize a Container object
### Issue
Currently, the following call is valid:

```sh
>>> from eth2spec.phase0 import spec
>>> x = spec.AttestationData(bar=1000)
>>> x
AttestationData(Container)
    slot: Slot = 0
    index: CommitteeIndex = 0
    beacon_block_root: Root = 0x0000000000000000000000000000000000000000000000000000000000000000
    source: Checkpoint = Checkpoint(Container)
                             epoch: Epoch = 0
                             root: Root = 0x0000000000000000000000000000000000000000000000000000000000000000
    target: Checkpoint = Checkpoint(Container)
                             epoch: Epoch = 0
                             root: Root = 0x0000000000000000000000000000000000000000000000000000000000000000
```

 I suppose it should have thrown an exception since `bar` is not a valid field in `AttestationData`. 

### How to solve it
Add check in `Container.__new__`
```python
        diff = set(kwargs.keys()).difference(set(fields.keys()))
        if len(diff) > 0:
            raise Exception(f'The field name {diff} is not defined in {cls}')
```

Comments:

Patch:
diff --git a/remerkleable/complex.py b/remerkleable/complex.py
index c92fa0c..e65160f 100644
--- a/remerkleable/complex.py
+++ b/remerkleable/complex.py
@@ -668,8 +668,12 @@ class Container(ComplexView):
                 raise Exception("cannot have both a backing and elements to init List")
             return super().__new__(cls, backing=backing, hook=hook, **kwargs)
 
+        diff = set(kwargs.keys()).difference(set(cls.fields().keys()))
+        if len(diff) > 0:
+            raise AttributeError(f'The field names {diff} are not defined in {cls}')
+
         input_nodes = []
-        for i, (fkey, ftyp) in enumerate(cls.fields().items()):
+        for fkey, ftyp in cls.fields().items():
             fnode: Node
             if fkey in kwargs:
                 finput = kwargs.pop(fkey)


Test patch:
diff --git a/remerkleable/test_typing.py b/remerkleable/test_typing.py
index 87fed19..dd20833 100644
--- a/remerkleable/test_typing.py
+++ b/remerkleable/test_typing.py
@@ -164,6 +164,12 @@ def test_container():
     except AttributeError:
         pass
 
+    try:
+        Foo(wrong_field_name=100)
+        assert False
+    except AttributeError:
+        pass
+
 
 def test_container_unpack():
     class Foo(Container):
Repository: protolambda/remerkleable

Issue:
Error when computing `__len__` generalized index on `List` type
The following code fails when using the [`get_generalized_indices` function defined in the `consensus-specs`](https://github.com/ethereum/consensus-specs/blob/cdb6725275ea9aaa6d4a323c72be59cc34b458a3/pysetup/spec_builders/altair.py#L30):

```python
    index = get_generalized_index(List[uint8, 256], "__len__")
    print(index)
```

with:

```
    if key >= cls.limit():
TypeError: '>=' not supported between instances of 'str' and 'int'
```

from here: https://github.com/protolambda/remerkleable/blob/91ed092d08ef0ba5ab076f0a34b0b371623db728/remerkleable/complex.py#L464

I would think this method should be extended to avoid this check for the special key `__len__`. Happy to open a PR if this is the preferred route!

Comments:

Patch:
diff --git a/remerkleable/bitfields.py b/remerkleable/bitfields.py
index 1434c8c..66b4e51 100644
--- a/remerkleable/bitfields.py
+++ b/remerkleable/bitfields.py
@@ -326,6 +326,8 @@ class Bitlist(BitsView):
 
     @classmethod
     def navigate_type(cls, key: Any) -> Type[View]:
+        if key == '__len__':
+            return uint256
         bit_limit = cls.limit()
         if key < 0 or key >= bit_limit:
             raise KeyError
diff --git a/remerkleable/complex.py b/remerkleable/complex.py
index 4574d16..5971925 100644
--- a/remerkleable/complex.py
+++ b/remerkleable/complex.py
@@ -461,6 +461,8 @@ class List(MonoSubtreeView):
 
     @classmethod
     def navigate_type(cls, key: Any) -> Type[View]:
+        if key == '__len__':
+            return uint256
         if key >= cls.limit():
             raise KeyError
         return super().navigate_type(key)


Test patch:
diff --git a/remerkleable/test_typing.py b/remerkleable/test_typing.py
index 4c8ee2a..6862414 100644
--- a/remerkleable/test_typing.py
+++ b/remerkleable/test_typing.py
@@ -347,12 +347,21 @@ def test_paths():
     assert (Wrapper / 'b' / 'quix').navigate_view(w) == 42
 
     assert (List[uint32, 123] / 0).navigate_type() == uint32
+    assert (List[uint32, 123] / "__len__").navigate_type() == uint256
     try:
         (List[uint32, 123] / 123).navigate_type()
         assert False
     except KeyError:
         pass
 
+    assert (Bitlist[123] / 0).navigate_type() == boolean
+    assert (Bitlist[123] / "__len__").navigate_type() == uint256
+    try:
+        (Bitlist[123] / 123).navigate_type()
+        assert False
+    except KeyError:
+        pass
+
 
 def test_bitvector():
     for size in [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 511, 512, 513, 1023, 1024, 1025]:
Repository: pyccel/pyccel

Issue:
Builtin abs function not working in c language
**Describe the bug**
Builtin abs function not implemented in c language

**To Reproduce**
Provide code to reproduce the behavior:
```python
@types('int')
def f(x):
    return abs(x)
```

**Error : **
ERROR at code generation stage
pyccel:
 |fatal [codegen]: first.py [3,11]| Pyccel has encountered syntax that has not been implemented yet. (<pyccel.ast.builtins.PythonAbs object at 0x7fa62ca792b0>)


**Language**
c


Comments:

Patch:
diff --git a/pyccel/codegen/printing/ccode.py b/pyccel/codegen/printing/ccode.py
index 45f6242a..9a5f4601 100644
--- a/pyccel/codegen/printing/ccode.py
+++ b/pyccel/codegen/printing/ccode.py
@@ -396,6 +396,17 @@ class CCodePrinter(CodePrinter):
 
     # ============ Elements ============ #
 
+    def _print_PythonAbs(self, expr):
+        if expr.arg.dtype is NativeReal():
+            self._additional_imports.add("math")
+            func = "fabs"
+        elif expr.arg.dtype is NativeComplex():
+            self._additional_imports.add("complex")
+            func = "cabs"
+        else:
+            func = "abs"
+        return "{}({})".format(func, self._print(expr.arg))
+
     def _print_PythonFloat(self, expr):
         value = self._print(expr.arg)
         type_name = self.find_in_dtype_registry('real', expr.precision)


Test patch:
diff --git a/tests/epyccel/test_builtins.py b/tests/epyccel/test_builtins.py
new file mode 100644
index 00000000..2b597fb1
--- /dev/null
+++ b/tests/epyccel/test_builtins.py
@@ -0,0 +1,41 @@
+# pylint: disable=missing-function-docstring, missing-module-docstring/
+
+from pyccel.epyccel import epyccel
+from pyccel.decorators import types
+
+def test_abs_i(language):
+    @types('int')
+    def f1(x):
+        return abs(x)
+
+    f2 = epyccel(f1, language=language)
+
+    assert f1(0) == f2(0)
+    assert f1(-5) == f2(-5)
+    assert f1(11) == f2(11)
+
+def test_abs_r(language):
+    @types('real')
+    def f1(x):
+        return abs(x)
+
+    f2 = epyccel(f1, language=language)
+
+    assert f1(0.00000) == f2(0.00000)
+    assert f1(-3.1415) == f2(-3.1415)
+    assert f1(2.71828) == f2(2.71828)
+
+
+
+def test_abs_c(language):
+    @types('complex')
+    def f1(x):
+        return abs(x)
+
+    f2 = epyccel(f1, language=language)
+
+    assert f1(3j + 4) == f2(3j + 4)
+    assert f1(3j - 4) == f2(3j - 4)
+    assert f1(5j + 0) == f2(5j + 0)
+    assert f1(0j + 5) == f2(0j + 5)
+    assert f1(0j + 0) == f2(0j + 0)
Repository: pydantic/pydantic

Issue:
Settings inheritance with ENV prefix does not work
# Bug

* OS: ubuntu 18.04
* Python version `import sys; print(sys.version)`: 3.7.5 (default, Nov  7 2019, 10:50:52) 
* Pydantic version `import pydantic; print(pydantic.VERSION)`: `1.0b1` or `1.2`, but works fine with `0.32`

```py
import os
from pydantic import BaseSettings


class MyBaseSettings(BaseSettings):
    var: int = 10


class MySubSettings(MyBaseSettings):
    class Config:
        env_prefix = 'PREFIX_'


os.environ['PREFIX_VAR'] = '2'
k = MySubSettings()
print(k.var)  # 10, but should be 2
```

We expect that `k.var` will be equal to 2, but it has default value 10.
It's reproduced since 1.0b1.


Comments:
I'm looking into this, but I'm afraid the solution isn't trivial.

Weird that this got 11 :+1: and 2 :eyes: in 2 hours. Any idea why this happened?

Patch:
diff --git a/pydantic/env_settings.py b/pydantic/env_settings.py
--- a/pydantic/env_settings.py
+++ b/pydantic/env_settings.py
@@ -66,7 +66,7 @@ class Config:
         @classmethod
         def prepare_field(cls, field: ModelField) -> None:
             env_names: Iterable[str]
-            env = field.field_info.extra.pop('env', None)
+            env = field.field_info.extra.get('env')
             if env is None:
                 if field.has_alias:
                     warnings.warn(
diff --git a/pydantic/fields.py b/pydantic/fields.py
--- a/pydantic/fields.py
+++ b/pydantic/fields.py
@@ -280,6 +280,7 @@ def infer(
     def set_config(self, config: Type['BaseConfig']) -> None:
         self.model_config = config
         info_from_config = config.get_field_info(self.name)
+        config.prepare_field(self)
         if info_from_config:
             self.field_info.alias = info_from_config.get('alias') or self.field_info.alias or self.name
             self.alias = cast(str, self.field_info.alias)


Test patch:
diff --git a/tests/test_settings.py b/tests/test_settings.py
--- a/tests/test_settings.py
+++ b/tests/test_settings.py
@@ -376,3 +376,18 @@ class SubSettings(Settings):
     assert SubSettings().dict() == {'foo': 'fff', 'bar': 'bbb', 'spam': 'spam default'}
     env.set('spam', 'sss')
     assert SubSettings().dict() == {'foo': 'fff', 'bar': 'bbb', 'spam': 'sss'}
+
+
+def test_prefix_on_parent(env):
+    class MyBaseSettings(BaseSettings):
+        var: str = 'old'
+
+    class MySubSettings(MyBaseSettings):
+        class Config:
+            env_prefix = 'PREFIX_'
+
+    assert MyBaseSettings().dict() == {'var': 'old'}
+    assert MySubSettings().dict() == {'var': 'old'}
+    env.set('PREFIX_VAR', 'new')
+    assert MyBaseSettings().dict() == {'var': 'old'}
+    assert MySubSettings().dict() == {'var': 'new'}
Repository: pydantic/pydantic

Issue:
cls is None in validator when validate_assignment is True
# Bug

When we set config value `validate_assignment` to True and define custom validator for a field, and try to set value for the field, we get None in `cls` argument in the validator method instead of expected model class object. In my case I want to store some data in model class object that I want to use for validation and I cannot get it via `cls` because `cls` is None. Of course I can use model class object directly, but this behavior is ambiguous and looks like a bug anyway.

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
         pydantic version: 1.3
        pydantic compiled: False
             install path: C:\Users\espdev\AppData\Local\pypoetry\Cache\virtualenvs\ms3eI9mF-py3.8\Lib\site-packages\pydantic
           python version: 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]
                 platform: Windows-10-10.0.18362-SP0
 optional deps. installed: []
```

A minimal example:

```py
from pydantic import BaseModel, StrictStr, validator

class Model(BaseModel):
    name: StrictStr

    class Config:
        validate_assignment = True

    @validator('name')
    def _validate_foo(cls, value):
        if cls is None:
            raise ValueError('cls is None')
        return value

if __name__ == '__main__':
    m = Model(name='hello')  # OK
    m.name = 'goodbye'       # <-- ValidationError "cls is None"
```
```
pydantic.error_wrappers.ValidationError: 1 validation error for Model
name
  cls is None (type=value_error)
```



Comments:

Patch:
diff --git a/pydantic/main.py b/pydantic/main.py
--- a/pydantic/main.py
+++ b/pydantic/main.py
@@ -284,7 +284,7 @@ def __setattr__(self, name, value):
         elif self.__config__.validate_assignment:
             known_field = self.__fields__.get(name, None)
             if known_field:
-                value, error_ = known_field.validate(value, self.dict(exclude={name}), loc=name)
+                value, error_ = known_field.validate(value, self.dict(exclude={name}), loc=name, cls=self.__class__)
                 if error_:
                     raise ValidationError([error_], type(self))
         self.__dict__[name] = value


Test patch:
diff --git a/tests/test_validators.py b/tests/test_validators.py
--- a/tests/test_validators.py
+++ b/tests/test_validators.py
@@ -1017,3 +1017,24 @@ def example_root_validator(cls, values):
     with pytest.raises(ValidationError):
         ModelB(a='a')
     assert not b_called
+
+
+def test_assignment_validator_cls():
+    validator_calls = 0
+
+    class Model(BaseModel):
+        name: str
+
+        class Config:
+            validate_assignment = True
+
+        @validator('name')
+        def check_foo(cls, value):
+            nonlocal validator_calls
+            validator_calls += 1
+            assert cls == Model
+            return value
+
+    m = Model(name='hello')
+    m.name = 'goodbye'
+    assert validator_calls == 2
Repository: pydantic/pydantic

Issue:
Same NameEmail field instances are not equal
# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: /Users/***/.local/share/virtualenvs/tempenv-42b2286175873/lib/python3.7/site-packages/pydantic
               python version: 3.7.4 (default, Jul 22 2019, 11:21:52)  [Clang 10.0.1 (clang-1001.0.46.4)]
                     platform: Darwin-18.7.0-x86_64-i386-64bit
     optional deps. installed: ['email-validator']
```

I'm assuming that equality should be enforced on field types such as `NameEmail`. At the moment it seems that instances that appear to be equal are generating different hashes. Unsure if this is desired, but I would argue it shouldn't be.

```py
import pydantic

# will raise an AssertionError
assert pydantic.NameEmail("test", "test@test.com") == pydantic.NameEmail("test", "test@test.com")
```



Comments:
Thanks for reporting.

Happy to accept a PR to add a proper `__eq__` method to `NameEmail`

Patch:
diff --git a/pydantic/networks.py b/pydantic/networks.py
--- a/pydantic/networks.py
+++ b/pydantic/networks.py
@@ -329,6 +329,9 @@ def __init__(self, name: str, email: str):
         self.name = name
         self.email = email
 
+    def __eq__(self, other: Any) -> bool:
+        return isinstance(other, NameEmail) and (self.name, self.email) == (other.name, other.email)
+
     @classmethod
     def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
         field_schema.update(type='string', format='name-email')


Test patch:
diff --git a/tests/test_networks.py b/tests/test_networks.py
--- a/tests/test_networks.py
+++ b/tests/test_networks.py
@@ -453,3 +453,5 @@ class Model(BaseModel):
 
     assert str(Model(v=NameEmail('foo bar', 'foobaR@example.com')).v) == 'foo bar <foobaR@example.com>'
     assert str(Model(v='foo bar  <foobaR@example.com>').v) == 'foo bar <foobaR@example.com>'
+    assert NameEmail('foo bar', 'foobaR@example.com') == NameEmail('foo bar', 'foobaR@example.com')
+    assert NameEmail('foo bar', 'foobaR@example.com') != NameEmail('foo bar', 'different@example.com')
Repository: pydantic/pydantic

Issue:
Can't use ~ in dotenv file in Settings
@HenryDashwood The issue here is that you use `~/.env`, which is not directly resolved by `pathlib.Path`.
You could change it to

```python
   class Config:
        env_file = os.path.expanduser('~/.env')
```

Maybe we could add `expanduser` directly in the code when `env_path` is created

_Originally posted by @PrettyWood in https://github.com/samuelcolvin/pydantic/issues/1368#issuecomment-669312462_


Comments:

Patch:
diff --git a/pydantic/env_settings.py b/pydantic/env_settings.py
--- a/pydantic/env_settings.py
+++ b/pydantic/env_settings.py
@@ -58,7 +58,7 @@ def _build_environ(
         env_file = _env_file if _env_file != env_file_sentinel else self.__config__.env_file
         env_file_encoding = _env_file_encoding if _env_file_encoding is not None else self.__config__.env_file_encoding
         if env_file is not None:
-            env_path = Path(env_file)
+            env_path = Path(env_file).expanduser()
             if env_path.is_file():
                 env_vars = {
                     **read_env_file(


Test patch:
diff --git a/tests/test_settings.py b/tests/test_settings.py
--- a/tests/test_settings.py
+++ b/tests/test_settings.py
@@ -1,4 +1,6 @@
 import os
+import uuid
+from pathlib import Path
 from typing import Dict, List, Optional, Set
 
 import pytest
@@ -547,6 +549,28 @@ class Config:
     assert s.pika == 'p!±@'
 
 
+@pytest.fixture
+def home_tmp():
+    tmp_filename = f'{uuid.uuid4()}.env'
+    home_tmp_path = Path.home() / tmp_filename
+    yield home_tmp_path, tmp_filename
+    home_tmp_path.unlink()
+
+
+@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
+def test_env_file_home_directory(home_tmp):
+    home_tmp_path, tmp_filename = home_tmp
+    home_tmp_path.write_text('pika=baz')
+
+    class Settings(BaseSettings):
+        pika: str
+
+        class Config:
+            env_file = f'~/{tmp_filename}'
+
+    assert Settings().pika == 'baz'
+
+
 @pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
 def test_env_file_none(tmp_path):
     p = tmp_path / '.env'
Repository: pydantic/pydantic

Issue:
validate_assignment with side effect validators 
# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.7.2
            pydantic compiled: True
                 install path: /home/arthur/Documents/odmantic/.venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.1 (default, Jan  5 2020, 23:12:00)  [GCC 7.3.0]
                     platform: Linux-4.17.14-041714-generic-x86_64-with-glibc2.27
     optional deps. installed: ['typing-extensions']
```

```py
from pydantic import root_validator, BaseModel

class Rectangle(BaseModel):
    width: float
    height: float
    area: float = None

    class Config:
        validate_assignment = True

    @root_validator()
    def set_ts_now(cls, v):
        v["area"] = v["width"] * v["height"]
        return v

r = Rectangle(width=1, height=1)
assert r.area == 1
r.height = 5
assert r.area == 5 # AssertionError, r.area has not been modified
```
I don't know if it should be allowed to use validators this way or if it's a real bug.
I'd be happy to try to implement a fix :smile: 


Comments:
Hi @art049 
Yes you're right. Since #1971 has been solved, this is the expected behaviour but it seems we update only the current field and not all. I can make a quick fix if you want should take couple of minutes

Patch:
diff --git a/pydantic/main.py b/pydantic/main.py
--- a/pydantic/main.py
+++ b/pydantic/main.py
@@ -406,7 +406,12 @@ def __setattr__(self, name, value):  # noqa: C901 (ignore complexity)
             if errors:
                 raise ValidationError(errors, self.__class__)
 
-        self.__dict__[name] = value
+            # update the whole __dict__ as other values than just `value`
+            # may be changed (e.g. with `root_validator`)
+            object_setattr(self, '__dict__', new_values)
+        else:
+            self.__dict__[name] = value
+
         self.__fields_set__.add(name)
 
     def __getstate__(self) -> 'DictAny':


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -666,6 +666,28 @@ def current_lessequal_500(cls, values):
     ]
 
 
+def test_root_validator_many_values_change():
+    """It should run root_validator on assignment and update ALL concerned fields"""
+
+    class Rectangle(BaseModel):
+        width: float
+        height: float
+        area: float = None
+
+        class Config:
+            validate_assignment = True
+
+        @root_validator
+        def set_area(cls, values):
+            values['area'] = values['width'] * values['height']
+            return values
+
+    r = Rectangle(width=1, height=1)
+    assert r.area == 1
+    r.height = 5
+    assert r.area == 5
+
+
 def test_enum_values():
     FooEnum = Enum('FooEnum', {'foo': 'foo', 'bar': 'bar'})
Repository: pydantic/pydantic

Issue:
BaseModel.copy(update=…) does not update __fields_set__
### Checks

* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug

# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.7.2
            pydantic compiled: False
                 install path: /usr/local/lib/python3.6/dist-packages/pydantic
               python version: 3.6.9 (default, Oct  8 2020, 12:12:24)  [GCC 8.4.0]
                     platform: Linux-4.15.0-112-generic-x86_64-with-Ubuntu-18.04-bionic
     optional deps. installed: ['typing-extensions', 'email-validator']
```

```py
from typing import Optional
import pydantic

class Foo(pydantic.BaseModel):
    foo: Optional[str]
    bar: Optional[str]

print(Foo(foo='hello')
      .copy(update={'bar': 'world'})
      .json(exclude_unset=True))
# Observed: {"foo": "hello"}
# Expected: {"foo": "hello", "bar": "world"}
```


Comments:

Patch:
diff --git a/pydantic/main.py b/pydantic/main.py
--- a/pydantic/main.py
+++ b/pydantic/main.py
@@ -627,7 +627,12 @@ def copy(
         cls = self.__class__
         m = cls.__new__(cls)
         object_setattr(m, '__dict__', v)
-        object_setattr(m, '__fields_set__', self.__fields_set__.copy())
+        # new `__fields_set__` can have unset optional fields with a set value in `update` kwarg
+        if update:
+            fields_set = self.__fields_set__ | update.keys()
+        else:
+            fields_set = set(self.__fields_set__)
+        object_setattr(m, '__fields_set__', fields_set)
         for name in self.__private_attributes__:
             value = getattr(self, name, Undefined)
             if value is not Undefined:


Test patch:
diff --git a/tests/test_construction.py b/tests/test_construction.py
--- a/tests/test_construction.py
+++ b/tests/test_construction.py
@@ -1,5 +1,5 @@
 import pickle
-from typing import Any, List
+from typing import Any, List, Optional
 
 import pytest
 
@@ -191,6 +191,14 @@ def test_copy_update():
     assert m != m2
 
 
+def test_copy_update_unset():
+    class Foo(BaseModel):
+        foo: Optional[str]
+        bar: Optional[str]
+
+    assert Foo(foo='hello').copy(update={'bar': 'world'}).json(exclude_unset=True) == '{"foo": "hello", "bar": "world"}'
+
+
 def test_copy_set_fields():
     m = ModelTwo(a=24, d=Model(a='12'))
     m2 = m.copy()