codecov-commenter: # [Codecov](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=h1) Report
> Merging [#49](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=desc) into [primary](https://codecov.io/gh/JoshData/python-email-validator/commit/458c9c49422eeac92903e4e7d2865b1256b361d6&el=desc) will **decrease** coverage by `0.63%`.
> The diff coverage is `33.33%`.

[![Impacted file tree graph](https://codecov.io/gh/JoshData/python-email-validator/pull/49/graphs/tree.svg?width=650&height=150&src=pr&token=HH5hPlDZRR)](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           primary      #49      +/-   ##
===========================================
- Coverage    69.86%   69.23%   -0.64%     
===========================================
  Files            1        1              
  Lines          219      221       +2     
===========================================
  Hits           153      153              
- Misses          66       68       +2     
```


| [Impacted Files](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [email\_validator/\_\_init\_\_.py](https://codecov.io/gh/JoshData/python-email-validator/pull/49/diff?src=pr&el=tree#diff-ZW1haWxfdmFsaWRhdG9yL19faW5pdF9fLnB5) | `69.23% <33.33%> (-0.64%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=footer). Last update [458c9c4...3b9ca41](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/email_validator/__init__.py b/email_validator/__init__.py
index 3b7014f..40140ca 100644
--- a/email_validator/__init__.py
+++ b/email_validator/__init__.py
@@ -156,6 +156,10 @@ class ValidatedEmail(object):
                        ) \
             + ")"
 
+    """Convenience method for accessing ValidatedEmail as a dict"""
+    def as_dict(self):
+        return self.__dict__
+
 
 def validate_email(
     email,
@@ -536,7 +540,7 @@ def main():
             email = email.decode("utf8")  # assume utf8 in input
         try:
             result = validate_email(email, allow_smtputf8=allow_smtputf8, check_deliverability=check_deliverability)
-            print(json.dumps(result, indent=2, sort_keys=True, ensure_ascii=False))
+            print(json.dumps(result.as_dict(), indent=2, sort_keys=True, ensure_ascii=False))
         except EmailNotValidError as e:
             if sys.version_info < (3,):
                 print(unicode_class(e).encode("utf8"))

TypeError: Object of type ValidatedEmail is not JSON serializable
As can be seen in `main()`:

https://github.com/JoshData/python-email-validator/blob/cc9b0019fc9d4cedb491fd760e092572858d4604/email_validator/__init__.py#L526-L527

It looks like running `email_validator` as a standalone module can only produce runtime errors due to the inability to serialize ValidatedEmail to a JSON object.

The easy fix is to use `result.__dict__` instead of `result`, which produces a dict representation of the class. 

I actually have another PR in the works that does some code cleanup functions, but wanted to capture it as an independent issue in case anyone else goes looking for this bug.
diff --git a/tests/test_main.py b/tests/test_main.py
index 7d5fe4b..d0c627a 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -250,6 +250,13 @@ def test_email_invalid(email_input, error_msg):
     assert str(exc_info.value) == error_msg
 
 
+def test_dict_accessor():
+    input_email = "testaddr@example.com"
+    valid_email = validate_email(input_email, check_deliverability=False)
+    assert isinstance(valid_email.as_dict(), dict)
+    assert valid_email.as_dict()["original_email"] == input_email
+
+
 def test_deliverability_no_records():
     assert validate_email_deliverability('example.com', 'example.com') == {'mx': [(0, '')], 'mx-fallback': None}
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest numpy>=1.16.0 pandas>=1.0.0
apt-get update apt-get install -y gcc
3.9
requirements/base.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
dnspython==2.7.0
-e git+https://github.com/JoshData/python-email-validator.git@458c9c49422eeac92903e4e7d2865b1256b361d6#egg=email_validator
exceptiongroup==1.2.2
idna==3.10
iniconfig==2.1.0
numpy==2.0.2
packaging==24.2
pandas==2.2.3
pluggy==1.5.0
pytest==8.3.5
python-dateutil==2.9.0.post0
pytz==2025.2
six==1.17.0
tomli==2.2.1
tzdata==2025.2

name: python-email-validator
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - dnspython==2.7.0
      - exceptiongroup==1.2.2
      - idna==3.10
      - iniconfig==2.1.0
      - numpy==2.0.2
      - packaging==24.2
      - pandas==2.2.3
      - pluggy==1.5.0
      - pytest==8.3.5
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - six==1.17.0
      - tomli==2.2.1
      - tzdata==2025.2
prefix: /opt/conda/envs/python-email-validator

tests/test_main.py::test_dict_accessor
tests/test_main.py::test_email_valid[Abc@example.com-output0] tests/test_main.py::test_email_valid[Abc.123@example.com-output1] tests/test_main.py::test_email_valid[user+mailbox/department=shipping@example.com-output2] tests/test_main.py::test_email_valid[!#$%&'*+-/=?^_`.{|}~@example.com-output3] tests/test_main.py::test_email_valid[\u4f0a\u662d\u5091@\u90f5\u4ef6.\u5546\u52d9-output4] tests/test_main.py::test_email_valid[\u0930\u093e\u092e@\u092e\u094b\u0939\u0928.\u0908\u0928\u094d\u092b\u094b-output5] tests/test_main.py::test_email_valid[\u044e\u0437\u0435\u0440@\u0435\u043a\u0437\u0430\u043c\u043f\u043b.\u043a\u043e\u043c-output6] tests/test_main.py::test_email_valid[\u03b8\u03c3\u03b5\u03c1@\u03b5\u03c7\u03b1\u03bc\u03c0\u03bb\u03b5.\u03c8\u03bf\u03bc-output7] tests/test_main.py::test_email_valid[\uf96e\u58eb\u8c6a@\u81fa\u7db2\u4e2d\u5fc3.tw-output8] tests/test_main.py::test_email_valid[jeff@\u81fa\u7db2\u4e2d\u5fc3.tw-output9] tests/test_main.py::test_email_valid[\uf96e\u58eb\u8c6a@\u81fa\u7db2\u4e2d\u5fc3.\u53f0\u7063-output10] tests/test_main.py::test_email_valid[jeff\uf96e@\u81fa\u7db2\u4e2d\u5fc3.tw-output11] tests/test_main.py::test_email_valid[\xf1o\xf1\xf3@example.com-output12] tests/test_main.py::test_email_valid[\u6211\u8cb7@example.com-output13] tests/test_main.py::test_email_valid[\u7532\u6590\u9ed2\u5ddd\u65e5\u672c@example.com-output14] tests/test_main.py::test_email_valid[\u0447\u0435\u0431\u0443\u0440\u0430\u0448\u043a\u0430\u044f\u0449\u0438\u043a-\u0441-\u0430\u043f\u0435\u043b\u044c\u0441\u0438\u043d\u0430\u043c\u0438.\u0440\u0444@example.com-output15] tests/test_main.py::test_email_valid[\u0909\u0926\u093e\u0939\u0930\u0923.\u092a\u0930\u0940\u0915\u094d\u0937@domain.with.idn.tld-output16] tests/test_main.py::test_email_valid[\u03b9\u03c9\u03ac\u03bd\u03bd\u03b7\u03c2@\u03b5\u03b5\u03c4\u03c4.gr-output17] tests/test_main.py::test_email_invalid[my@.leadingdot.com-An tests/test_main.py::test_email_invalid[my@\uff0e\uff0eleadingfwdot.com-An tests/test_main.py::test_email_invalid[my@..twodots.com-An tests/test_main.py::test_email_invalid[my@twodots..com-An tests/test_main.py::test_email_invalid[my@baddash.-.com-The tests/test_main.py::test_email_invalid[my@baddash.-a.com-The tests/test_main.py::test_email_invalid[my@baddash.b-.com-The tests/test_main.py::test_email_invalid[my@example.com\n-The tests/test_main.py::test_email_invalid[my@example\n.com-The tests/test_main.py::test_email_invalid[.leadingdot@domain.com-The tests/test_main.py::test_email_invalid[..twodots@domain.com-The tests/test_main.py::test_email_invalid[twodots..here@domain.com-The tests/test_main.py::test_email_invalid[me@\u2488wouldbeinvalid.com-The tests/test_main.py::test_email_invalid[@example.com-There tests/test_main.py::test_email_invalid[\nmy@example.com-The tests/test_main.py::test_email_invalid[m\ny@example.com-The tests/test_main.py::test_email_invalid[my\n@example.com-The tests/test_main.py::test_email_invalid[11111111112222222222333333333344444444445555555555666666666677777@example.com-The tests/test_main.py::test_email_invalid[111111111122222222223333333333444444444455555555556666666666777777@example.com-The tests/test_main.py::test_email_invalid[me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444444444455555555556.com-The tests/test_main.py::test_email_invalid[my.long.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.11111111112222222222333333333344444.info-The tests/test_main.py::test_email_invalid[my.long.address@\u03bb111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.11111111112222222222333333.info-The tests/test_main.py::test_email_invalid[my.long.address@\u03bb111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444.info-The tests/test_main.py::test_email_invalid[my.\u03bbong.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444.info-The tests/test_main.py::test_email_invalid[my.\u03bbong.address@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444.info-The tests/test_main.py::test_deliverability_no_records tests/test_main.py::test_deliverability_found tests/test_main.py::test_deliverability_fails tests/test_main.py::test_deliverability_dns_timeout
email_validator/__init__.py
email_validator/__init__.py